我试图创建一个从servlet获取数据的骨干模型的简单示例。
我有这个:
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)});
我的servlet是这样的:
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.
}
}
模型内部的解析函数不会执行,也不会执行done方法。但是,fail方法可以正常返回响应对象。
答案 0 :(得分:0)
您的JSON字符串无效。它应该是
"{"id":1,"title":"harry","data":"sdsd"}"
还要确保根据操作结果返回成功的HTTP状态代码,否则将调用错误回调。