javascript Backbone.js 获取回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10038441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:31:47  来源:igfitidea点击:

Backbone.js fetch callback

javascriptbackbone.js

提问by dzm

I'm using Backbone.js and using fetch with options, but it doesn't seem to get the error or success callbacks, however data is being returned.

我正在使用 Backbone.js 并使用带选项的 fetch,但它似乎没有得到错误或成功回调,但是正在返回数据。

this.user.fetch({data: {username : this.username.val(), check : 'true'}}, {
    error: function(model, response) {
        console.log(response);
    },
    success: function(model, response)  {
        console.log(response);
    }
});

This is what I have setup, am I missing something? It never hits error or success, but it does do the ajax request and it's returning data.

这是我设置的,我错过了什么吗?它永远不会出错或成功,但它确实会执行 ajax 请求并返回数据。

Thank you!

谢谢!

回答by x1a4

You're passing 2 separate arguments to fetch. Combine them into a single object with data, success, and errorfields and it should work for you.

您将 2 个单独的参数传递给fetch. 将它们与datasuccesserror字段组合成一个对象,它应该适合您。

回答by MKK

Example for x1a4 answer

x1a4 答案示例

var myModel = new MyModel({id: modelId});
myModel.fetch({
  success: function (model) {
    console.log('success: model fetched');
  },
  error: function () {
    console.log('error: loading model');
  }
});