5

我有以下代码来为集合创建新模型。底层数据存储是一个远程 API:

        var postCreationStatus = this.model.create(newPostModel, {
            wait : true     // waits for server to respond with 200 before adding newly created model to collection
        }, {
            success : function(resp){
                console.log('success callback');
                console.log(resp);
            },
            error : function(err) {
                console.log('error callback');
                console.log(err);
            }
        });

新模型被创建,我可以从数据库中确认这一点,但是成功和错误回调都没有被调用。

创建完成后,我想重定向用户。重定向过早地终止了 AJAX 请求,这就是为什么我使用成功回调很重要。

服务器以 JSON 响应{ id : 11 }和 HTTP 状态响应200 OK

4

1 回答 1

6

查看主干代码,我意识到我对该create()函数的调用不正确。成功和错误回调需要在作为第二个参数传入的对象中,而不是作为第三个参数。更改后的工作片段是这样的:

var postCreationStatus = this.model.create(newPostModel, {
    wait : true,    // waits for server to respond with 200 before adding newly created model to collection

    success : function(resp){
        console.log('success callback');
        console.log(resp);
        that.redirectHomePage();
    },
    error : function(err) {
        console.log('error callback');
        // this error message for dev only
        alert('There was an error. See console for details');
        console.log(err);
    }
});
于 2012-11-28T07:45:20.767 回答