Sah*_*aka 3 html javascript php ajax
这是我的index.html
<script>
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);
</script>
Run Code Online (Sandbox Code Playgroud)
这是我的ajax.php
<?php
echo "Hello " . $_POST["name"];
?>
Run Code Online (Sandbox Code Playgroud)
当我在我的localhost上运行这个结果时
注意:未定义的索引:第2行的C:\ xampp\htdocs\chat\ajax.php中的名称 你好
但是当我使用JQuery时它正在正常工作......
我的问题是如何在没有JQuery的情况下使用JavaScript发送ajax ..?
当您对发布数据使用ajax时,您必须传递正确的标头信息以及请求
<script>
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");
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
420 次 |
| 最近记录: |