Rusty Rob
Rusty Rob

Reputation: 17173

why won't my backbone model fetch from the server

request handler for '/donut'

class DonutHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write("{id: 42, name: 'bob', age: 12}")

Javascript:

var Donut = Backbone.Model.extend({
    url: '/donut',
    urlRoot: '/donut'
});

donut = new Donut()

donut.fetch({success: function() {
                      console.log('ok');
             },
             error: function(collection, response) {
                      console.log('error on -> ' + response.responseText);
             }
 });

error on -> {id: 42, name: 'bob', age: 12}

I expected instead that 'ok' would be logged to the console and that my donut would now have attributes age, id and name.

Upvotes: 0

Views: 293

Answers (2)

nikoshr
nikoshr

Reputation: 33344

Your JSON is not valid, see http://json.org/, keys names must be a string and values must adhere to certain rules.

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes

and

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Try sending {"id": 42, "name": "bob", "age": 12}

Upvotes: 1

Erez Rabih
Erez Rabih

Reputation: 15788

That happens because your response is not a valid JSON string.

Try to send this as a response:

"{\"id\": \"42\", \"name\": \"bob\", \"age\": \"12\"}"

Upvotes: 3

Related Questions