jQuery扩展ajax参数

发布于 2024-11-17 14:22:39 字数 1890 浏览 1 评论 0原文

哦嘿,

我有一个中央函数,可以在我的订单中运行 AJAX 请求。我有默认设置(本质上是 ajaxSetup,但我不想使用它),然后每个函数传递自己的设置/参数。 * 使用 jQuery 1.6.1

唯一的问题是我使用 $.extend (深度扩展和正常工作都不是)。看来扩展并没有带来某些功能。奇怪的是,这曾经有效,但现在不起作用,我不明白为什么:|。 ajax 请求确实运行,但成功后没有任何反应...我在这个实例中使用 $.when(this.sendData(params, 'snapshotCache')).then(do stuff); 调用 sendData

这是我的代码:

    this.sendData = function(options, cacheIndex) {

  var defaults = {
    url: this.dataPath + options.action,
    data: 'qs=NULL',
    type: 'POST',
    dataType: 'text/html', //(THIS SHOULD HAVE BEEN just 'text')
    success: function(result, textStatus, xhr) {
      console.log(cacheIndex)
      console.log('HELLO')
      if(typeof cacheIndex !== 'undefined') {
        var qs = settings.data;
        cacheObj.cacheAdd(qs, cacheIndex, result);
        alert(cacheObj);
      }
    },
    error: function(xhr, textStatus, errorThrown) {
      if(textStatus === 'timeout') {
         $.ajax(this); // retry
         return;
       } else if(errorThrown === 500) {
        alert(ERROR['500Msg']);
       }
    }
  };
  var settings = $.extend(true, defaults, options);
  return $.ajax(settings);
};

这是我返回之前对象设置中的内容:

url => /order/getsnapshot
data => micro=img%2Fstore%2Fcomp-08.png%2Cimg%2Fstore%2Fcomp-08n.png%2Cimg%2Fstore%2Fcomp-08t.png%2Cimg%2Fstore%2Fcomp-08a.png%2Cimg%2Fstore%2Fcomp-08l.png%2Cimg%2Fstore%2Fcomp-08h.png&outPath=1242353103103.png
type => POST
dataType => text/html
success => function (result, textStatus, xhr) { console.log(cacheIndex); console.log("HELLO"); if (typeof cacheIndex !== "undefined") { var qs = settings.data; alert(cacheObj); } }
error => function (xhr, textStatus, errorThrown) { if (textStatus === "timeout") { $.ajax(this); return; } else if (errorThrown === 500) { alert(ERROR['500Msg']); } }
action => getsnapshot
cache => true

Ohey,

I have a central function that runs AJAX requests throughout my order form. I have default settings (essentially ajaxSetup, but I didn't want to use that), and then each function passes in its own settings/parameters. * USING jQuery 1.6.1

Only issue is I'm using $.extend (neither deep extend nor normal works). It seems extending doesn't carry along some of the functions. The weird thing is this worked once but now it doesnt and I can't figure out why :|. The ajax request does run, but nothing happens on success... I call sendData in this instance with $.when(this.sendData(params, 'snapshotCache')).then(do stuff);

Here's my code:

    this.sendData = function(options, cacheIndex) {

  var defaults = {
    url: this.dataPath + options.action,
    data: 'qs=NULL',
    type: 'POST',
    dataType: 'text/html', //(THIS SHOULD HAVE BEEN just 'text')
    success: function(result, textStatus, xhr) {
      console.log(cacheIndex)
      console.log('HELLO')
      if(typeof cacheIndex !== 'undefined') {
        var qs = settings.data;
        cacheObj.cacheAdd(qs, cacheIndex, result);
        alert(cacheObj);
      }
    },
    error: function(xhr, textStatus, errorThrown) {
      if(textStatus === 'timeout') {
         $.ajax(this); // retry
         return;
       } else if(errorThrown === 500) {
        alert(ERROR['500Msg']);
       }
    }
  };
  var settings = $.extend(true, defaults, options);
  return $.ajax(settings);
};

Here's what is in the object settings before I return:

url => /order/getsnapshot
data => micro=img%2Fstore%2Fcomp-08.png%2Cimg%2Fstore%2Fcomp-08n.png%2Cimg%2Fstore%2Fcomp-08t.png%2Cimg%2Fstore%2Fcomp-08a.png%2Cimg%2Fstore%2Fcomp-08l.png%2Cimg%2Fstore%2Fcomp-08h.png&outPath=1242353103103.png
type => POST
dataType => text/html
success => function (result, textStatus, xhr) { console.log(cacheIndex); console.log("HELLO"); if (typeof cacheIndex !== "undefined") { var qs = settings.data; alert(cacheObj); } }
error => function (xhr, textStatus, errorThrown) { if (textStatus === "timeout") { $.ajax(this); return; } else if (errorThrown === 500) { alert(ERROR['500Msg']); } }
action => getsnapshot
cache => true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-11-24 14:22:39

不需要调用$.when()。下面就足够了:

this.sendData(params, 'snapshotCache')).done(function(){ ... });

请记住新的延迟,done() 就像成功,fail() 就像错误。

另外,我只会为您的设置对象执行此操作:

var settings = {};
$.extend(true, settings, defaults, options);

Calling $.when() is not needed. Below is sufficient:

this.sendData(params, 'snapshotCache')).done(function(){ ... });

Keep in mind with the new deferreds, done() is like success and fail() is like error.

Also, I would just do this for your settings objects:

var settings = {};
$.extend(true, settings, defaults, options);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文