[英]how can i insert data using javascript fetch api
我正在尝试使用fetch
获取数据以形成我的view page
结果:
var_dump($_POST)
然后结果show array(0) {}
表示空数组
function newCustomerAdd(){
url = "data.php";
const x = {
name : "hasan",
roll : 1
}
fetch(url,{
method : "POST",
body : JSON.stringify(x),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
}).then(function(response){
return response.text();
}).then(function(text){
console.log(text);
}).catch(function(error){
console.log(error);
})
}
但是var_dump($_POST)
然后结果show array(0) {}
意味着空数组
我的问题如何从我的view page
获取数据
JavaScript Code
function newCustomerAdd(){
url = "data.php";
let x = {
name : "hasan",
roll : 1
}
fetch(url,{
method : "POST",
body : JSON.stringify(x),
headers: {
'Content-Type': 'application/text'
},
}).then(function(response){
return response.text();
}).then(function(text){
console.log(text);
}).catch(function(error){
console.log(error);
})
}
PHP code
只需获取数据
$content = json_decode(file_get_contents("php://input"));
print_r($content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.