GET方式
<code> document.getElementById("save").onclick=function(){ var request=new XMLHttpRequest(); request.open("GET","http://localhost/service.php?username="+document.getElementById("stuname").value+"&number="+document.getElementById("stunumber").value+"&sex="+document.getElementById("stusex").value+"&job="+document.getElementById("stujob").value); request.send(null); request.onreadystatechange=function(){ if(request.readyState===4){ if(request.status===200){ document.getElementById("saveResult").innerHTML=request.responseText; }else{ alert("发生错误la :"+request.status); } } } } service.php $username=$_GET['username']; $password=$_GET["number"]; $sex=$_GET["sex"]; $job=$_GET["job"]; if(($username=="11")&&($password)&&($sex)&&($job)){ echo '{"success":"true","msg":"学生添加成功"}'; } else{ echo '{"success":"false","msg":"参数错误,填写信息不全"}'; } 结果:是正确的。 但是,换为post传递,就有问题,代码如下: document.getElementById("save").onclick=function(){ var request=new XMLHttpRequest(); var data="name1="+document.getElementById("stuname").value+"&number="+document.getElementById("stunumber").value+"&sex="+document.getElementById("stusex").value+"&job="+document.getElementById("stujob").value; request.open("POST","http://localhost/service.php"); request.setRequestHeader("Content-Type","appliaction/x-www-form-urlencoded"); request.send(data); request.onreadystatechange=function(){ if(request.readyState===4){ if(request.status===200){ document.getElementById("saveResult").innerHTML=request.responseText; } else{ alert("发生错误la :"+request.status); } } } } service.php代码: var_dump($_SERVER['REQUEST_METHOD']);//为了测试用 $username=$_POST['name1']; $password=$_POST["number"]; $sex=$_POST["sex"]; $job=$_POST["job"]; if(($username=="11")&&($password)&&($sex)&&($job)){ echo '{"success":"true","msg":"学生添加成功"}'; } else{ echo '{"success":"false","msg":"参数错误,填写信息不全"}'; } </code>
结果为:
string(4) "POST"
Notice: Undefined index: name1 in D:phpstudyWWWservice.php on line 23
Notice: Undefined index: number in D:phpstudyWWWservice.php on line 24
Notice: Undefined index: sex in D:phpstudyWWWservice.php on line 25
Notice: Undefined index: job in D:phpstudyWWWservice.php on line 26
{"success":"false","msg":"参数错误,填写信息不全"}
,这个是为什么呢?我哪里写的有问题?请高人指点,谢谢!
GET方式
<code> document.getElementById("save").onclick=function(){ var request=new XMLHttpRequest(); request.open("GET","http://localhost/service.php?username="+document.getElementById("stuname").value+"&number="+document.getElementById("stunumber").value+"&sex="+document.getElementById("stusex").value+"&job="+document.getElementById("stujob").value); request.send(null); request.onreadystatechange=function(){ if(request.readyState===4){ if(request.status===200){ document.getElementById("saveResult").innerHTML=request.responseText; }else{ alert("发生错误la :"+request.status); } } } } service.php $username=$_GET['username']; $password=$_GET["number"]; $sex=$_GET["sex"]; $job=$_GET["job"]; if(($username=="11")&&($password)&&($sex)&&($job)){ echo '{"success":"true","msg":"学生添加成功"}'; } else{ echo '{"success":"false","msg":"参数错误,填写信息不全"}'; } 结果:是正确的。 但是,换为post传递,就有问题,代码如下: document.getElementById("save").onclick=function(){ var request=new XMLHttpRequest(); var data="name1="+document.getElementById("stuname").value+"&number="+document.getElementById("stunumber").value+"&sex="+document.getElementById("stusex").value+"&job="+document.getElementById("stujob").value; request.open("POST","http://localhost/service.php"); request.setRequestHeader("Content-Type","appliaction/x-www-form-urlencoded"); request.send(data); request.onreadystatechange=function(){ if(request.readyState===4){ if(request.status===200){ document.getElementById("saveResult").innerHTML=request.responseText; } else{ alert("发生错误la :"+request.status); } } } } service.php代码: var_dump($_SERVER['REQUEST_METHOD']);//为了测试用 $username=$_POST['name1']; $password=$_POST["number"]; $sex=$_POST["sex"]; $job=$_POST["job"]; if(($username=="11")&&($password)&&($sex)&&($job)){ echo '{"success":"true","msg":"学生添加成功"}'; } else{ echo '{"success":"false","msg":"参数错误,填写信息不全"}'; } </code>
结果为:
string(4) "POST"
Notice: Undefined index: name1 in D:phpstudyWWWservice.php on line 23
Notice: Undefined index: number in D:phpstudyWWWservice.php on line 24
Notice: Undefined index: sex in D:phpstudyWWWservice.php on line 25
Notice: Undefined index: job in D:phpstudyWWWservice.php on line 26
{"success":"false","msg":"参数错误,填写信息不全"}
,这个是为什么呢?我哪里写的有问题?请高人指点,谢谢!
post 用$_POST取
如果你又想取get 还想取post 可以用 $_REQUEST
因为你 PHP 中是用$_GET 所以当然只能获取get传递过来的值
$_GET 获取 url连接中的参数
$_POST 获取 post提交的参数
$_REQUEST 可以获取到get和post的参数
你的案例里,用$_GET是获取不到Post参数的。
post请求时参数格式不正确,换成var postData = { "name1": "value1", "name2": "value2"} 这样试一试。可以F12调试一下。