是否可以通过修改 beforeSend 回调中的 XMLHttpRequest 对象来修改 Ajax 请求中发送的数据?如果是这样,我该怎么做?
Is it possible to modify the data sent in an Ajax request by modifying the XMLHttpRequest object in the beforeSend callback? and if so how might I do that?
可以修改,beforeSend 是 实际上 (在 jQuery 1.4+ 中):
Yes you can modify it, the signature of beforeSend is actually (in jQuery 1.4+):
beforeSend(XMLHttpRequest, settings)
即使文档只有 beforeSend(XMLHttpRequest),你可以在这里看到它是怎么调用的,这里s 是设置对象:
even though the documentation has just beforeSend(XMLHttpRequest), you can see how it's called here, where s is the settings object:
if ( s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false ) {
因此,您可以在此之前修改 data 参数(请注意,此时它已经是一个字符串,即使你传入了一个对象).修改它的示例如下所示:
So, you can modify the data argument before then (note that it's already a string by this point, even if you passed in an object). An example of modifying it would look like this:
$.ajax({
//options...
beforeSend: function(xhr, s) {
s.data += "&newProp=newValue";
}
});
如果有帮助,同样的签名适用于 .ajaxSend() 全局处理程序(确实 有正确的 文档 显示它),例如这个:
If it helps, the same signature applies to the .ajaxSend() global handler (which does have correct documentation showing it), like this:
$(document).ajaxSend(function(xhr, s) {
s.data += "&newProp=newValue";
});
这篇关于是否可以从 beforeSend 回调中修改 XMLHttpRequest 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)