是否有可能从beforeSend回调修改XMLHtt prequest数据? [英] Is it possible to modify XMLHttpRequest data from beforeSend callback?
问题描述
是否有可能通过修改XMLHtt prequest对象中的beforeSend回调修改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(XMLHtt prequest)
的你可以看到它是如何在这里叫,其中的 取值
是设置对象:
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 ) {
那么,你的能在那之前(的的修改数据
参数https://github.com/jquery/jquery /blob/1.4.4/src/ajax.js#L207-209">note,它的的已的字符串由这点的,即使你在一个对象通过)。修改它的一个例子是这样的:
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回调修改XMLHtt prequest数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!