[英]AJAX not posting values with $(form).serialize()
I have a form that I'm trying to submit via AJAX. 我有一个要通过AJAX提交的表格。 The easiest way for me to pass the data would be using $("#myForm").serialize()
, however, when doing so, the page to which I'm posting to doesn't receive the data. 对于我来说,传递数据的最简单方法是使用$("#myForm").serialize()
,但是,这样做时,我要发布到的页面没有收到数据。
Here's my form: 这是我的表格:
<form id="myForm">
<input name="field" id="field">
<button id="submitBtn" type="button">
</form>
And this is my function: 这是我的功能:
$("#submitBtn").click(function(){
alert($("#myForm").serialize()) //For testing – does alert "field=value"
var post = $.post("actions.php", $("#myForm").serialize());
post.done(function(d){alert(d)}); //Only alerts [PHPSESSID]
var post = $.post("actions.php", {field:"fieldVal"});
post.done(function(d){alert(d)}); //Alerts [PHPSESSID] and ['field']
});
This is my whole actions.php
file: 这是我的整个actions.php
文件:
<?php
print_r($_REQUEST);
exit();
Why is passing the values as JSON working but .serialize()
isn't?? 为什么将值作为JSON传递但.serialize()
不起作用?
Looks like I just had to pass the serialized form as a variable instead of serializing it inside the $.post()
function. 看起来我只需要将序列化的表单作为变量传递,而不是在$.post()
函数中对其进行序列化。 As so: 因此:
var postData = $("#myForm").serialize()
var post = $.post("actions.php", postData);
post.done(function(d){alert(d)});
Not sure why it works when established outside and not inside the function, maybe a conflict issue. 不确定在函数外部而不是函数内部建立时为何起作用,这可能是冲突问题。 Thanks to everyone anyway 还是谢谢大家
请改用serializeArray()
: https : //api.jquery.com/serializeArray/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.