0

When I call the function sites.fetch() in the google console this error appears Cross-Origin Read Blocking (CORB) blocked cross-origin response https://api.nutritionix.com/v1_1/search?callback=jQuery351004426279547866718_1648579753369&\_=1648579753370 with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.i read about it and seems i need to add some headers but i don't know how to add on backbone.js.

Here my code:

App = {};

App.Site = Backbone.Model.extend();
App.Sites = Backbone.Collection.extend({
    url: "https://api.nutritionix.com/v1_1/search",
    model: App.Site,
    fetch: function(options){
        options = options || {};
        options.dataType = 'jsonp';
        Backbone.Collection.prototype.fetch.call(this, options);
    }
    
});

App.LoadSites = Backbone.View.extend({
    initialize: function(){
        this.listenTo(this.collection, 'add', this.renderOne);
        this.listenTo(this.collection, 'reset', this.renderAll);
        this.listenTo(this.collection, 'sync', this.renderAll);
    },

    tagName: 'ul',
    className: 'Sites',
    render: function(){
        this.collection.trigger('reset');
        return this;
    },

    renderAll: function(){
        this.$el.empty();
        this.collection.forEach(this.renderOne);
    },

    renderOne: function(site){
            loadSite = new LoadSite();
            loadSite.model = site;
            loadSite.render().$el.appendTo(loadSites.el);
    }

});


App.LoadSite = Backbone.View.extend({
    template: _.template('<p><strong><%= text %></strong></p>'),
    tagName: 'li',
    className: 'Site',

    render: function(){
        var innerHTML = this.template({text: this.model.get('text')});
        this.$el.html(innerHTML);
        return this;
    }

});


var loadSite, sites;

$(function() {
    
    sites = new App.Sites([]);

    loadSites = new App.LoadSites({
        collection: sites
    });

    loadSites.render().$el.appendTo('#back');
    console.log('veiw rend')
})

I tried other things but I couldn't solve it.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • You said `options.dataType = 'jsonp'` (which is a specially formatted JavaScript application) but the server isn't responding with JSONP, it is responding with JSON. It errors instead of trying to execute the JSON as JavaScript. – Quentin Mar 30 '22 at 09:24
  • so I should use "JSON.parse()" instead of "options.dataType = 'jsonp'"? – Arthur Lima Silva Mar 30 '22 at 12:15
  • I'd be surprised if Backbone didn't have support for automatically parsing JSON. – Quentin Mar 30 '22 at 13:40
  • I changed the 'jsonp' to 'json' and added an "options.contentType="application/json";" and now this error appears "GET https://api.nutritionix.com/v1_1/search 500 (Internal Server Error)" do you know what it could be? – Arthur Lima Silva Mar 30 '22 at 17:08

0 Answers0