加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax传输json到后台

发布时间:2020-12-16 01:55:16 所属栏目:百科 来源:网络整理
导读:前端: script$.fn.serializeObject=function(){varo={};vara=this.serializeArray();$.each(a,function(){if(o[this.name]){if(!o[this.name].push){o[this.name]=[o[this.name]];}o[this.name].push(this.value||'');}else{o[this.name]=this.value||'';}}

前端:

<script>
$.fn.serializeObject=function()
{
varo={};
vara=this.serializeArray();
$.each(a,function(){
if(o[this.name]){
if(!o[this.name].push){
o[this.name]=[o[this.name]];
}
o[this.name].push(this.value||'');
}else{
o[this.name]=this.value||'';
}
});
returno;
};
document.getElementById("hand").onclick=function(){
varrequest=newXMLHttpRequest();
request.open("POST","../controller/post.php");
varjsonuserinfo=$('#form1').serializeObject();
vardata=JSON.stringify(jsonuserinfo);
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send(data);
request.onreadystatechange=function(){
if(request.readyState===4){
if(request.status===200){
document.getElementById("postResult").innerHTML=request.responseText;
}else{
alert("发生错误:"+request.status);
}
}
}
}
</script>

后台:

<?php
error_reporting(0);
require_once('../model/PdoMySQL.class.php');
require_once('../model/config.php');
header("Content-Type:text/html;charset=utf-8");
if($_SERVER["REQUEST_METHOD"]=="POST"){
create();
}
//创建笼位申请信息
functioncreate(){
//判断信息是否填写完全
$data=file_get_contents("php://input");
...
//忽略后面代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读