这是我的index.html
var xml = new XMLHttpRequest();
xml.onreadystatechange = function(){
if (xml.readyState === 4 && xml.status === 200) {
console.log(xml.responseText);
}
}
xml.open("POST", "ajax.php", true);
var data = new FormData();
data.append("name","Sahan");
xml.send(data);
这是我的ajax.php
echo "Hello " . $_POST["name"];
?>
当我在我的localhost上运行这个结果时
Notice: Undefined index: name in C:\xampp\htdocs\chat\ajax.php on line 2
Hello
但是当我使用JQuery时它正在正常工作……
我的问题是如何在没有JQuery的情况下使用JavaScript发送ajax ..?
解决方法:
当您对发布数据使用ajax时,您必须传递正确的标头信息以及请求
var xml = new XMLHttpRequest();
xml.onreadystatechange = function(){
if (xml.readyState === 4 && xml.status === 200) {
console.log(xml.responseText);
}
}
xml.open("POST", "ajax.php", true);
//Send the proper header information along with the request
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
/*var data = new FormData();
data.append("name","Stackoverflow");*/
xml.send("name=Stackoverflow");
标签:html,javascript,php,ajax
来源: https://codeday.me/bug/20190724/1522096.html