无法从jQuery Ajax请求中访问数据,返回空数组 [英] Cannot access data from jQuery Ajax request, returns empty array
本文介绍了无法从jQuery Ajax请求中访问数据,返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个通过 fancybox插件登录示例调用的表单。
以下是我的代码:
表格:
< form method =postaction =id =events_form>
< p class =clearfix>< label for =Name>名称:< / label> < input type =textname =Nameid =Name/>< / p>
< p class =clearfix>< label for =Company>公司:< / label> < input type =textname =Companyid =Company/>< / p>
< p class =clearfix>< label for =电子邮件>电子邮件:< / label> < input type =textname =Emailid =Email/>< / p>
< p class =clearfix>< label for =Tel> Tel:< / label> < input type =textname =Telid =Tel/>< / p>
< p class =clearfix>< input type =submitvalue =提交详情/>< / p>
< / form>
JavaScript / jQuery:
< script type =text / javascript>
$(document).ready(function(){
$(#event_trigger)。fancybox({$ b $'padding':0,
'scrolling':'no ',
'titleShow':false,
});
$(#events_form)。bind(submit,function(){
$ .fancybox.showActivity();
$ .ajax({
type:POST,
cache:false,
url:/ events / index。 php,
data:$(this).serializeArray(),
success:function(data){
$ .fancybox(data);
}
} );
返回false;
});
});
< / script>
PHP文件返回并为空数组。然而 Firebug 帖子标签会显示表单数据。
另外,我注意到如果我这样做
$ b $ pre $ print_r($ _ SERVER ['REQUEST_METHOD'] )
这会返回GET,即使我指定了POST。
<解决方案
>
,其形式为CSS id(在本例中为#my-form-ID)的名称,如下所示:
$(#my-form-ID)。serializeArray()
希望解决它。它为我工作。 ; - D
I have a form that is called via the fancybox plugin login example.
Here is the code I have:
Form:
<form method="post" action="" id="events_form">
<p class="clearfix"><label for="Name">Name:</label> <input type="text" name="Name" id="Name" /></p>
<p class="clearfix"><label for="Company">Company:</label> <input type="text" name="Company" id="Company" /></p>
<p class="clearfix"><label for="Email">Email:</label> <input type="text" name="Email" id="Email" /></p>
<p class="clearfix"><label for="Tel">Tel:</label> <input type="text" name="Tel" id="Tel"/></p>
<p class="clearfix"><input type="submit" value="Submit details" /></p>
</form>
JavaScript / jQuery:
<script type="text/javascript">
$(document).ready(function(){
$("#event_trigger").fancybox({
'padding' : 0,
'scrolling' : 'no',
'titleShow' : false,
});
$("#events_form").bind("submit", function() {
$.fancybox.showActivity();
$.ajax({
type : "POST",
cache : false,
url : "/events/index.php",
data : $(this).serializeArray(),
success: function(data) {
$.fancybox(data);
}
});
return false;
});
});
</script>
The PHP file returns and empty array. However the Firebug post tab displays the form data.
Also, I noticed that if I do
print_r($_SERVER['REQUEST_METHOD'])
This returns GET, even though I have specified POST.
解决方案
$(this).serializeArray()
with the name of the form CSS id (#my-form-ID, in this example) like this:
$("#my-form-ID").serializeArray()
Hope that solves it. It worked for me. ;-D
这篇关于无法从jQuery Ajax请求中访问数据,返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文