当我从ember-rails转移到外部ember应用程序时,我使用具有不同URL的ember数据,所以API是http://localhost:5000,而ember应用程序是http://localhost:9000。
现在的问题是我需要包含x-appid和x-app-secret,但是只要为它们中的任何一个添加xhr.setRequestHeader(),GET请求就变成了OPTIONS请求。
当我在同一个域上使用ember-rails时,这段代码工作得很好,这是问题所在,还是缺少其他东西?
ajax: function(url, type, hash) {
if (this.headers !== undefined) {
var headers = this.headers;
if (hash) {
hash.beforeSend = function (xhr) {
// Works fine
xhr.setRequestHeader('Accept', 'application/vnd.vendor+json;version=1');
// Changes Request from GET to OPTIONS
xhr.setRequestHeader('x-vendor-appid', '12412412');
xhr.setRequestHeader('x-vendor-secret', 'aslkdfjaskldfjasd');
};
}
}
return this._super(url, type, hash);
}发布于 2013-06-17 22:53:49
浏览器正在发送OPTIONS请求,因为请求的域与页面的域不匹配。这会触发Cross-Origin Resource Sharing (CORS)限制。
您需要响应来自服务器的CORS请求,通过设置几个HTTP header字段来告诉客户端允许向该域发出CORS请求。然后,浏览器将发出原始请求。我已经用ember.js/rails实现了这一点。
https://stackoverflow.com/questions/17144138
复制相似问题