php post方法获取Ajax后台传来的数据失败,但是用php get方法却可以获取到。

web_1_3_1_4 2017-10-17 11:59:47
这是html文档

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>

<script type="text/javascript">
window.onload = function(){
var btn = document.getElementById("btn");
btn.onclick = function(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

var username = document.getElementById("username").value;
var password = document.getElementById('password').value;

var url = "post.php";

xhr.open('post',url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

var param = 'username='+username+'&password='+password;
alert(param)
xhr.send(param);

xhr.onreadystatechange = function(){
if(xhr.readyState==4){
if(xhr.status==200){
var data = xhr.responseText;
console.log(data);
document.getElementById("content").innerHTML = data;
}
}
}

}

}


</script>

</head>
<body>
<form>
用户名: <input type="text" id="username"/>
密码: <input type="password" id="password"/>
<input type="button" value="提交" id="btn"/>

</form>
<div id="content"></div>

</body>
</html>

这是 php 文档

<?php

header("Content-Type:text/html;charset=utf-8");

//$username = $_GET["username"];
//$password = $_GET["password"];

$username = $_POST['username'];
$password = $_POST['password'];


echo "用户名:".$username."密码:".$password;




?>

...全文
530 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2017-10-19
  • 打赏
  • 举报
回复
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); 去掉红色的内容试试,不过就算content-type错,也不会把参数加到url上的,检查你的服务器是不是做了什么转换

Web开发学习资料推荐
ajax对象属性withCredentials
javascript生成二维码

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧