我知道人们可能会对为什么我不使用rails感到非常困惑,但我使用 php 感觉更好,因此我选择了它。我基本上是在尝试创建一个非常简单的backbone.js。我已经预定义了urlRoot和url函数。我已经对 php 进行了编码,只是简单地返回一条消息(使用echo)。
但无论我做什么,每当我尝试接收响应时,它总是落入错误回调。我确实得到了responseText作为响应,但是我仍然不明白为什么会触发错误回调。这是我的完整html和后端php代码。为什么总是去错误回调?我还想声明我收到标题
HTTP/1.1 200 OK
HTML(里面也有 Backbone.js);
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
</head>
<body>
<div id='place'>
<input id='clicker' type='button' value='Click Me'/>
</div>
<script type='text/javascript'>
(function($){
Backbone.emulateHTTP=true;
Backbone.emulateJSON=true;
var URL=Backbone.Model.extend({
initialize:function(){
console.log("URL object has been created");
},
defaults:{
url:'not actually defined'
},
urlRoot:'/StupidExample/server.php',
url:function(){
var base=this.urlRoot || (this.collection && this.collection.url) || "/";
if(this.isNew()) return base;
return base+"?id="+encodeURIComponent(this.id);
}
});
var URLView=Backbone.View.extend({
initialize:function(){
console.log('URL View has been created');
},
el:('#place'),
events:{
"click #clicker":"alerter"
},
alerter:function(){
console.log("I've been clicked");
var obj=new URL();
obj.save(obj.toJSON,{
success:function(){
console.log("Success")
},
error:function(model,response,xhr){
console.log(model);
console.log(response);
console.log(xhr);
console.log("Error");
}
});
}
});
var urlView=new URLView();
})(jQuery);
</script>
</body>
</html>
PHP;
<?php
echo "I've received something";
?>