Ember.js ember-data restadapter fail to load json

前端 未结 3 1428
不思量自难忘°
不思量自难忘° 2020-12-25 08:48

Cheers! I have ember-data store:

TravelClient.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({ bulkCommit: false, url: \"http://         


        
3条回答
  •  生来不讨喜
    2020-12-25 08:55

    The RESTAdapter expects JSON that is not the problem but the page and the json are not on the same domain, this is a security issue. You can resolve this by using one of the two solutions named below.

    You're running into the same origin policy you should either use JSONP or CORS. The quickest fix would probably be to tell ember-data that you want to use JSONP.

    For CORS your server needs to respond to an OPTIONS request with the headers:

    • Access-Control-Allow-Origin
    • Access-Control-Request-Method

    i'm no rails expert but you will probably need to do something with the gem rack-cors see here or here.

    You could do that by overriding the ajax hook in the RESTAdapter like so:

    App.store = DS.Store.create({
        revision: 11,
        adapter: DS.RESTAdapter.create({
            namespace: "data",
            url: "",
            ajax: function (url, type, hash) {
                hash.url = url;
                hash.type = type;
                hash.dataType = 'jsonp';
                hash.contentType = 'application/json; charset=utf-8';
                hash.context = this;
    
                if (hash.data && type !== 'GET') {
                    hash.data = JSON.stringify(hash.data);
                }
    
                jQuery.ajax(hash);
            },
        })
    });
    

提交回复
热议问题