的X请求,使用包头没有jQuery中当作ajaxForm插件设置 [英] X-Requested-With header not set in jquery ajaxForm plugin
问题描述
我使用的是的jQuery插件ajaxForms 做一个ajax向我CakePHP的应用程序。
I'm using the jQuery ajaxForms plugin to make an ajax submit to my CakePHP app.
蛋糕的RequestHandler的通过观察检测Ajax请求的X-要求,以头,但形式的插件似乎并没有将其设置。或者jQuery的不使用插件时设置。
Cake's RequestHandler detects ajax requests by looking at the "X-Requested-With" header, but the forms plugin does not seem to set it. Or jQuery does not set it when using the plugin.
我试了几件事情,
在主要的onload功能我补充说:
in the main onload function i added:
$.ajaxSetup({
headers: {"X-Requested-With":"XMLHttpRequest"}
});
在插件code我加了这一权利的实际AJAX调用之前:
In the plugin code I added this right before the actual ajax call:
options.beforeSend = function(xhr) {
xhr.setRequestHeader("X_REQUESTED_WITH", "XMLHttpRequest");
};
进行一次普通的Ajax调用,并设置页眉...
Making a regular ajax call, does set the header...
谁能告诉我这是怎么回事,或最重要的,我怎么能解决这个问题?
Can anyone tell me what's going on or most important, how can I fix this?
推荐答案
@Nicky德Maeyer的回答了自己的问题
@Nicky De Maeyer's answer to his own question
其实你不必这样做自己(附加一个隐藏的输入字段)。
Actually you don't need to do this yourself (appending a hidden input field).
AFAIK你可以这样的数据传递到ajaxForms插件的选项对象
AFAIK you can just pass such data to ajaxForms plugin in the options object
$('#myForm1').ajaxForm({data:{"X_REQUESTED_WITH":"XMLHttpRequest"}});
应该自动地(在隐藏的iframe的文件上传时)追加这样的输入表单上提交
Should automagically (in the hidden iframe file upload case) append such an input to your form on submission
<input type="hidden" name="X_REQUESTED_WITH" value="XMLHttpRequest" />
这篇关于的X请求,使用包头没有jQuery中当作ajaxForm插件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!