1

我一直在尝试从我的流星代码访问 osTicket api,到目前为止,这是我的代码:

if (Meteor.isServer) {
Meteor.methods({
    osTicket: function() {
        this.unblock();
        return HTTP.post("http://www.xxxxxxxx.com/uploads/api/tickets.json","X-API-Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

            {
                data: {
                    "alert": true,
                    "autorespond": true,
                    "source": "API",
                    "name": "Angry",
                    "email": "api@osticket.com",
                    "phone": "3185558634X123",
                    "subject": "Testing API",
                    "ip": "172.22.78.114",
                    "message": "MESSAGE HERE",
                    "attachments": [{
                        "file.txt": "data:text/plain;charset=utf-8,content"
                    }, {
                        "image.png": "data:image/png;base64,R0lGODdhMAA..."
                    }, ]
                },
            },
            function(error, results) {
                if (results) {
                    console.log(results);
                } else {
                    console.log(error)
                }
            }
        );
    }
});
}

if (Meteor.isClient) {

    Template.api.events({
        'click #submitQuery': function() {

            Meteor.call("osTicket");

        }
    })

}

我从 api 获得 200 状态和一些 html 代码,这意味着连接成功,但我无法使用 api 从我的代码创建任何票证。

那么,我做错了什么?我与 api 连接的语法是否正确?

请参阅https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api/tickets.md了解更多信息。

谢谢。

4

1 回答 1

1

使用纤维/未来的npm 包。

添加meteorhacks:npm包meteor

meteor add http
meteor add meteorhacks:npm

创建 packages.json 并添加纤维

{
  "fibers": "1.0.7",
}

看:

if (Meteor.isServer) {
  var Future = Meteor.npmRequire('fibers/future'); 

  Meteor.methods({
    osTicket: function() {

    // Create our future instance.
      var future = new Future();
          data = {
              "alert": true,
              "autorespond": true,
              "source": "API",
              "name": "Angry",
              "email": "api@osticket.com",
              "phone": "3185558634X123",
              "subject": "Testing API",
              "ip": "172.22.78.114",
              "message": "MESSAGE HERE",
              "attachments": [
                { "file.txt": "data:text/plain;charset=utf-8,content" }, 
                { "image.png": "data:image/png;base64,R0lGODdhMAA..." }, 
              ]
          };

      return HTTP.post("http://www.xxxxxxxx.com/uploads/api/tickets.json","X-API-Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", { data:  data },
        function(error, response) {
          if (error) {
            return future.return(error);
          } else {
            future.return( response );
          }
      });

      return future.wait();
    }
  });
}

if (Meteor.isClient) {
  Template.api.events({
    'click #submitQuery': function() {
      Meteor.call("osTicket", function(error, response) {

        console.log(response);
      });
    }
  })
}
于 2015-11-16T09:05:27.247 回答