0
votes

iam trying to create a simple example of backbone model fetching data from a servlet.

I have this:

require(["backbone"],function(Backbone)


var myDataModel = new (Backbone.Model.extend({

    initialize:function(){
        this.set({title:"My First Backbone",data:new Date()});
    },

    setTitle:function(title){       
        this.set({title:title});        
    },

    setData:function(data){     
        this.set({data:data});      
    },

    getTitle:function(){        
        return this.get("title");       
    }, 

    getData:function(){     
        return this.get("data");        
    },
    url:"http://........../BackboneApp/Todo",

    parse:function(response,xhr){


        console.log(response);

    }

}));

myDataModel.fetch().done(function(s){console.log(s)});

and my servlet is this:

public class TodoServlet extends HttpServlet  {


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String text = "{id:1,title:'harry',data:'sdsd'}";
    response.setContentType("application/json");  // Set content type of the response so that jQuery knows what it can expect.

    response.getWriter().write(text);       // Write response body.
}

}

The parse function inside model does not execute and neither does the done method.But the fail method works fine returning the response object.

1
What is the HTTP status code you are returning? - T J

1 Answers

0
votes

Your JSON string is invalid. It should be

"{"id":1,"title":"harry","data":"sdsd"}"

Also make sure you are returning a success HTTP status code depending on the result of operation, otherwise the error callback will be invoked.