干杯! 我有 ember 数据存储:
TravelClient.Store = DS.Store.extend({
revision: 11,
adapter: DS.RESTAdapter.create({ bulkCommit: false, url: "http://someIP:somePORT"})
});
和路由器:
TravelClient.ToursRoute = Ember.Route.extend({
model: function() {
return TravelClient.Tour.find();
}
});
我从远程服务器收到这个 JSON:
{
"tours": [
{
"id": "5110e8b5a8fefe71e0000197",
"title": "qui deserunt dolores",
"description": "Id velit nihil.",
"seats": 12,
"options": [
],
"images": [
{
"id": "5110e8b5a8fefe71e0000196",
"url": "url"
}
}
但是当我尝试return TravelClient.Tour.find()
它失败时:
http://someIP:somePORT/tours 404 (Not Found)
XMLHttpRequest cannot load http://someIP:somePORT/tours. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
它似乎 RESTAdapter 不知道,它必须接收 JSON 还是什么?
更新:
在 Rails 服务器端的应用程序控制器中:
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
end
但它仍然是:
OPTIONS http://someIP:somePORT/tours 404 (Not Found)
看起来 RESTAdapter 试图加载 tours 资源,而不是 tours.json:
Request URL:http://someIP:somePORT/tours
工作解决方案
扩展 RESTAdapter:
TravelClient.CUSTOMAdapter = DS.RESTAdapter.extend({
bulkCommit: false,
url: "http://remote_server_address",
buildURL: function(record, suffix) {
var s = this._super(record, suffix);
return s + ".json";
}
})
并使用正确的标头响应 OPTIONS 请求