嵌套$ .ajax回调未触发 [英] nested $.ajax callback not firing

查看:77
本文介绍了嵌套$ .ajax回调未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我有一个具有两种主要方法的对象"save"和"relate",它们都使用jQuery ajax调用到达服务器.

这两种方法都将回调函数作为参数,但是即使在Chrome开发人员工具中查看所有的保存/关联"信息并从服务器返回有效数据,也只会触发一种回调方法.

这是我正在使用的三个部分:

Entity.prototype.save = function(callback)
{
    var self = this;
    $.ajax({
        url: '/Entity/Create',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(model),
        success: function (data){},
        error: function (xhr, status, err) { },
        complete: function (data)
        {
             console.log(self.get('_id') + ' saved.')
             if(callback)
                  callback(data.responseText);
        }
   });
 }

 Entity.prototype.relate = function(relatedEntityId, isRelate, callback)
 {
     var self = this;
     var action = isRelate ? 'Relate' : 'Unrelate';

     $.ajax({
         url: '/Entity/' + action,
         type: 'POST',
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         data: JSON.stringify({ primaryEntityId: self._id.value, relatedEntityId: relatedEntityId }),
         done: function (data) { },
         fail: function (xhr, status, err) { },
         always: function (data)
         {
             console.log(self.get('_id') + 'related to ' + relatedEntityId);
             if(callback)
                 callback(data.responseText);
         }
     });
 }


 // from main code:

 entity.save(function (id) // this callback fires
 {
     // request is a previously saved 'entity'
     request.relate(id, true, function (id) // this callback does not
     {
         console.log('related callback completed.');
     });
 });

当以相同的方式设置$ .ajax调用时(在"complete"处理程序中触发了回调.),我不确定为什么第二个回调(来自request.relate)没有触发. /p>

我正在测试最新版本的Chrome(25.0.1364.172 m).也已经在Firefox 19中进行了测试,结果相同.

解决方案

done failalways无效的ajax属性,它们用于延迟的对象.

您需要使用错误/成功/完成.

I'm working on a project where I've got an object with two main methods, "save" and "relate", both hitting the server using jQuery ajax calls.

Both methods take callback functions as parameters, but only one of the callback methods is firing, even though looking in Chrome dev tools all of the Save/Relate posts are happening and returning valid data from the server.

Here are the three pieces I'm working with:

Entity.prototype.save = function(callback)
{
    var self = this;
    $.ajax({
        url: '/Entity/Create',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(model),
        success: function (data){},
        error: function (xhr, status, err) { },
        complete: function (data)
        {
             console.log(self.get('_id') + ' saved.')
             if(callback)
                  callback(data.responseText);
        }
   });
 }

 Entity.prototype.relate = function(relatedEntityId, isRelate, callback)
 {
     var self = this;
     var action = isRelate ? 'Relate' : 'Unrelate';

     $.ajax({
         url: '/Entity/' + action,
         type: 'POST',
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         data: JSON.stringify({ primaryEntityId: self._id.value, relatedEntityId: relatedEntityId }),
         done: function (data) { },
         fail: function (xhr, status, err) { },
         always: function (data)
         {
             console.log(self.get('_id') + 'related to ' + relatedEntityId);
             if(callback)
                 callback(data.responseText);
         }
     });
 }


 // from main code:

 entity.save(function (id) // this callback fires
 {
     // request is a previously saved 'entity'
     request.relate(id, true, function (id) // this callback does not
     {
         console.log('related callback completed.');
     });
 });

I'm not sure why the second callback (from request.relate) doesn't fire, when the $.ajax calls are set up in the same way (with the callback being fired in the 'complete' handler.

I'm testing in the latest version of Chrome (25.0.1364.172 m). Also have tested in Firefox 19 with same result.

解决方案

done fail and always are not valid ajax properties, they are for deferred objects.

You need to use error/success/complete.

这篇关于嵌套$ .ajax回调未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆