php无法获取ajax的值,javascript – 无法在ajax请求中获取PHP中的$_POST值

这是我的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值