我試圖使用Meteor的(v1.0)HTTP.call
方法與基於Python的服務器進行通信,該服務器只接受頭文件中的application/json
內容類型但從客戶端調用API URL時,我無法在Meteor中正確設置HTTP標頭。在客戶端的HTTP.call中設置「Content-Type」頭文件流星
有了這樣一個片段,我從Python的服務器415 (Unsupported Media Type)
錯誤:
if (Meteor.isClient) {
Template.testing.events({
'click button': function(event, tpl) {
event.preventDefault();
var method = 'GET';
var url = 'http://localhost:6543/test';
var options = {
headers: {'Content-Type': 'application/json'}
}
HTTP.call(method, url, options, function(error, result) {
if (error) {
console.log('ERRR');
console.log(error);
} else
console.log('RESULT');
console.log(result);
});
}
});
}
但是,如果我叫從服務器端相同的URL流星像這樣:
if (Meteor.isClient) {
Template.testing.events({
'click button': function(event, tpl) {
event.preventDefault();
var method = 'GET';
var url = 'http://localhost:6543/test';
var options = {
headers: {'Content-Type': 'application/json'}
}
Meteor.call('APICall', method, url, options, function (error, result) {
if (error) {
console.log('CLIENT ERRR');
console.log(error);
} else {
console.log('CLIENT RESULT');
console.log(result);
}
});
}
});
}
if (Meteor.isServer) {
Meteor.methods({
APICall: function (method, url, options) {
HTTP.call(method, url, options, function(error, result) {
if (error) {
console.log('SERVER ERRR');
console.log(error);
} else
console.log('SERVER RESULT');
console.log(result);
});
}
});
}
我從服務器得到了正確的迴應。
在Python方面,我爲所有可能的請求啓用了CORS起源(例如cors_origins=('*')
)。
所以...是可以在客戶端設置標題,還是應該始終從服務器端調用此服務?
謝謝你的回答!想知道,你提到「你的書」?那是哪一個? – errata 2014-10-31 11:52:28
啊! [這是一個成語](http://idioms.thefreedictionary.com/in+my+book)。雖然我希望我真的寫了一本書:) – 2014-11-01 11:17:35
啊哈哈,我看到:)從來沒有聽說過迄今爲止哈哈哈哈:) – errata 2014-11-02 23:34:41