我一直试图从我的流星代码访问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连接语法是否正确?
Plz请参阅https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api/tickets.md了解更多信息。
感谢。
答案 0 :(得分: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);
});
}
})
}