简体   繁体   English

Backbone.js:奇怪的.save()错误回调

[英]Backbone.js: Weird .save() error callback

I get an error response from the .save() function. 我从.save()函数收到错误响应。 The server side php returns me a text and successfully saves the url into database. 服务器端php向我返回文本,并将URL成功保存到数据库中。 When I inspect the response I can see the text returned by PHP, however I do not understand why it turns in the error callback. 当我检查响应时 ,可以看到PHP返回的文本,但是我不明白为什么它会出现在错误回调中。 What could be the reason? 可能是什么原因?

urlToAdd.save({}, {
    success: function () {
        console.log("In here");
    },
    error: function (model, response) {
        console.log(model.toJSON());
        console.log(response);
        console.log("Not in here");
    }
});

The server responds with: 服务器响应:

HTTP/1.1 200 OK
Date: Sat, 19 May 2012 21:31:27 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.6
X-Powered-By: PHP/5.3.6
Content-Length: 96
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
X-Pad: avoid browser bug

Hmmm... Even though the response is valid, the content-type tells the whole story... The expected content-type should be application/json. 嗯...即使响应是有效的,内容类型也能说明整个故事...预期的内容类型应为application / json。 If your response isn't JSON, error will be invoked every time. 如果您的响应不是JSON,则每次都会调用错误。 If you have a way to encode your response as JSON, you should see the success invoked instead of the error. 如果您有一种将响应编码为JSON的方法,则应该看到调用成功而不是错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM