jQuery扩展ajax参数
哦嘿,
我有一个中央函数,可以在我的订单中运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要调用
$.when()
。下面就足够了:请记住新的延迟,
done()
就像成功,fail()
就像错误。另外,我只会为您的设置对象执行此操作:
Calling
$.when()
is not needed. Below is sufficient:Keep in mind with the new deferreds,
done()
is like success andfail()
is like error.Also, I would just do this for your settings objects: