开发者

jQuery extend ajax parameters

开发者 https://www.devze.com 2023-03-16 03:36 出处:网络
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

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 cacheInde开发者_如何学运维x !== "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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消