我记得我去年使用jQuery ajax帖子,我只需键入$_POST [‘param’]即可获得帖子数据.今天我再次使用它来进行项目,所以我加载了最新的jQuery lib并使用了以下代码:
<script type="text/javascript"> $(document).on('submit','.ajaxform',function(e){ e.preventDefault(); var datastring = $(this).serialize(); $.ajax({ type: "POST",data: datastring,contentType: "application/json; charset={CHARSET}",dataType: "json",beforeSend: function(){ console.log($(this).serialize()); },success: function(data){ alert('yes'); },error: function(errMsg){ alert('error'); } }); }); </script> <form action="" id="register" name="register" class="ajaxform" method="post" enctype="multipart/form-data"> <input type="text" name="username" value="" /> <input type="password" name="password" value="" /> <input type="hidden" name="action" value="register" /> <input type="hidden" name="{TOKENID}" value="{TOKEN}" /> <input type="submit" value="Register" /> </form>
提交后,我无法通过键入$_POST [‘username’]来获取输入字段值.
我检查了检查元素,我注意到有些不同.
Request URL:http://project0/member.PHP Request Method:POST Status Code:200 OK ... ... Content-Type:application/json; charset=UTF-8 ... ... Request payload: username=asd&password=asd&action=register&29cd5e5ca1=73050db390872eb7c3fc564206b961c59a6363d6
除了:
原文链接:https://www.f2er.com/php/135393.htmlcontentType: "application/json; charset={CHARSET}"
使用(内容类型的默认值):
contentType: "application/x-www-form-urlencoded; charset=UTF-8"