我正在尝试使用fetch()
API POST 方法以获取 PHP 中的 POST 数据。
这是我尝试过的:
var x = "hello";
fetch(url,{method:'post',body:x}).then(function(response){
return response.json();
});
PHP:
<?php
if(isset($_GET['x']))
{
$get = $_GET['x'];
echo $get;
}
?>
它是否正确?
这取决于:
如果你想$_GET['x']
,您需要在查询字符串中发送数据:
var url = '/your/url?x=hello';
fetch(url)
.then(function (response) {
return response.text();
})
.then(function (body) {
console.log(body);
});
如果你想$_POST['x']
,您需要将数据发送为FormData
:
var url = '/your/url';
var formData = new FormData();
formData.append('x', 'hello');
fetch(url, { method: 'POST', body: formData })
.then(function (response) {
return response.text();
})
.then(function (body) {
console.log(body);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)