php – jQuery ajax回到$_POST而不是REQUEST有效负载
发布时间:2020-12-13 18:19:30 所属栏目:PHP教程 来源:网络整理
导读:我记得我去年使用jQuery ajax帖子,我只需键入$_POST [‘param’]即可获得帖子数据.今天我再次使用它来进行项目,所以我加载了最新的jQuery lib并使用了以下代码: script type="text/javascript" $(document).on('submit','.ajaxform',function(e){ e.prevent
我记得我去年使用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 这就是为什么我只能输入$_POST [‘param’]来获取POST数据?如何返回POST数据而不是有效负载?
除了:
contentType: "application/json; charset={CHARSET}" 使用(内容类型的默认值): contentType: "application/x-www-form-urlencoded; charset=UTF-8" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |