简体   繁体   中英

Backbone.js: Save Method Always Return Error Callback

I know people probably will get real confused why I am not using but I feel better using php thus I've picked it. 但我觉得使用php更好,所以我选择了它。 I'm basically trying to create a really simple . I've predefined the and functions. 函数。 I've coded php to just simply return me a message (using ). )。

But no matter what I do, whenever I try receive the response it always falls to the callback. 回调。 I do get the in response, however I still cannot understand why the error callback is triggered. 的responseText,但是我仍然无法理解为什么会触发错误回调。 Here is my full and back-end code. 和后端代码。 Why is it always going to the error callback? I would also like to state I receive header

HTTP/1.1 200 OK

(also with Backbone.js inside); (内置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
echo "I've received something";
?>

You must return a valid JSON object with your HTTP 200 response. Backbone throws an error on your echo return value because it tries to parse the response as JSON, which fails.

As Derick said you need to return JSON, which you can do by changing your php file to:

 <?php print json_encode(array( 'response' => 'success' ));

But you will also need to not use absolute paths as your URL because AJAX is not allowed across domains so it's required to be relative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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