是否可以通过修改beforeSend回调中的XMLHttpRequest对象来修改Ajax请求中发送的数据?如果是这样的话,我该怎么做呢?
发布于 2010-12-25 17:53:45
可以修改,beforeSend的签名实际上是(在jQuery 1.4+中):
beforeSend(XMLHttpRequest, settings)即使文档中只有beforeSend(XMLHttpRequest)、you can see how it's called here和s is the settings object
if ( s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false ) {因此,您可以在此之前修改data参数(note that it's a string by this point,即使您传入了一个对象)。修改它的示例如下所示:
$.ajax({
//options...
beforeSend: function(xhr, s) {
s.data += "&newProp=newValue";
}
});如果有帮助,相同的签名将应用于.ajaxSend()全局处理程序(具有显示它的正确documentation ),如下所示:
$(document).ajaxSend(function(xhr, s) {
s.data += "&newProp=newValue";
});发布于 2015-05-13 14:19:58
我一直在寻找这个解决方案,我想知道为什么我找不到数据,所以我将请求类型更改为post,但它就在那里,看起来如果您使用的是GET请求,s.data属性就不在那里,我猜您必须更改s.url
对于get方法:
$.ajax({
type:'GET',
beforeSend: function(xhr, s) {
s.url += "&newProp=newValue";
}
});对于post方法:
$.ajax({
type:'POST',
beforeSend: function(xhr, s) {
s.data += "&newProp=newValue";
}
});https://stackoverflow.com/questions/4527054
复制相似问题