Is it possible to modify XMLHttpRequest data from beforeSend callback?(是否可以从 beforeSend 回调中修改 XMLHttpRequest 数据?)
问题描述
是否可以通过修改 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 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以从 beforeSend 回调中修改 XMLHttpRequest 数据?


基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01