我需要从外部服务获取数据。它有API。这是一个例子: http://portal.example.com/portal.api?l=username&p=keyphrase&act=brand_by_nr&nr=kl2&alt
参数是: “l” - 登录,“p” - 密码,“行动” - 执行功能,“nr” - 部件号
我尝试通过Meteor http连接。这是我的服务器代码:
var sources = {
mskv: {
url: "http://portal.example.com/portal.api",
auth: { l: "mylogin", p: "cBKoTyalCgbOQb37NG6sbb0qv2I0Q4PmWRJIJMWpOhCPFombqeDv7fBhdkjsdhkjah" },
params: { act: "brand_by_nr", nr: null }
}
};
Meteor.methods({
doRequest: function(partNumber) {
for (var key in sources) {
var url = sources[key].url;
var authData = sources[key].auth;
var paramsData = sources[key].params;
paramsData.nr = partNumber;
HTTP.call("POST", url, { auth: authData, params: paramsData }, function(err, res) {
if (err) {
throw new Meteor.Error("not-response", "Remote server not responding");
}
return res;
});
}
}
});
这是我的客户代码:
Template.search.events({
"click .search": function(event) {
var partNumber = document.getElementsByClassName("input")[0].value;
Meteor.call("doRequest", partNumber, function(err, res) {
if(err === "not-response") return;
console.log(res);
});
}
});
我有错误:
> Exception while invoking method 'doRequest' TypeError: Object
> #<Object> has no method 'indexOf' I20150227-00:01:35.455(3)? at Object._call (packages/http/httpcall_server.js:42:1)
> I20150227-00:01:35.455(3)? at Object._.extend.wrapAsync [as call]
> (packages/meteor/helpers.js:118:1) I20150227-00:01:35.455(3)? at
> [object Object].Meteor.methods.doRequest (app/server/server.js:19:18)
你能帮助我,我的错误在哪里?
答案 0 :(得分:1)
尝试
var paramsData = [sources[key].params];
我怀疑它正在寻找阵列。
答案 1 :(得分:0)
在我的情况下,auth字段不正确。真正的方法是auth:&#34;登录:密码&#34;,看起来就像字符串一样。第二个错误 - 不需要auth字段。对于此服务,登录和密码作为参数发送{params:{l:&#34; login&#34;,p:&#34; password&#34;,act:&#34; brand_by_nr&#34; ....}}