【问题标题】:How do I access osTicket api from meteorjs?如何从 meteorjs 访问 osTicket api?
【发布时间】:2016-02-17 05:11:54
【问题描述】:

我一直在尝试从我的流星代码访问 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 了解更多信息。

谢谢。

【问题讨论】:

    标签: javascript api meteor osticket


    【解决方案1】:

    使用fibers/future npm 包。

    添加meteorhacks:npm包流星

    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);
          });
        }
      })
    }
    

    【讨论】:

    • 谢谢,我也想知道我是否在正确的地方使用了 api 密钥? (作为 http.POST 的参数)还是我必须将它放在代码的任何其他部分?
    • 为什么不使用 HTTP.get ?和 meteor 添加 http 包?
    • 但我的要求是创建一张票,所以我需要通过 post 方法发送一些数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多