Art Taylor
Art Taylor

Reputation: 492

Why does this ajax call work with jQuery but not backbone.sync

Before I start, I know that the response is not jsonp, but json.

So the following code produces a result:

$.ajax({
    type: 'GET', 
     url: 'http://us.battle.net/api/wow/character/Alexstrasza/Leica?fields=items,stats,talents',
     dataType: 'jsonp', 
    success: function(data){
        console.log(data);

     },
    error: function() { console.log('Uh Oh!'); },
    statusCode: { 
        404: function() {
            $().html("not working");
        }
    },
     jsonp: 'jsonp'
    });

However when I would do the exact same call with backbone I get the error "Uncaught SyntaxError: Unexpected token :" (ie, its not jsonp)

define([
'jquery',
'backbone',
'models/results/panel.model'
], 
function(
    $, 
    Backbone,
    PanelModel
){

var BoxCollection = Backbone.Collection.extend({

    model: PanelModel,

    url: function(){

        return "http://us.battle.net/api/wow/character/Alexstrasza/Leica?fields=items,stats,talents";

    },

    initialize: function(models, options){

        this.path = options.path;
    },

    sync: function(method, model, options){
    options.timeout = 10000; 
        options.dataType = "jsonp"; 
    options.processData = true;
    options.type = 'READ';
        options.url = this.url();   
    return Backbone.sync(method, model, options);

    },


    parse: function(resp, options){
        console.log(resp);
    }

});

return BoxCollection;

});

From my understandings, Backbone uses jQuery.ajax as an underlying method for all ajax calls and I would think that they would act the same, but apparently thats not the case.

Thank you

Upvotes: 0

Views: 142

Answers (1)

nikoshr
nikoshr

Reputation: 33344

Your Backbone version is missing the jsonp parameter. Try

sync: function(method, model, options){
    options.timeout = 10000; 
    options.dataType = "jsonp"; 
    options.processData = true;
    options.type = 'READ';
    options.url = this.url();
    options.jsonp = 'jsonp';
    return Backbone.sync(method, model, options);
}

and a demo http://jsfiddle.net/nikoshr/8A6n4/

Upvotes: 1

Related Questions