本文介绍了如何通过POST方法发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要一种通过 POST 方法发送数据的方法,但响应为空.为什么?
I just need a way to send data via POST method, but the response is empty. Why ?
React Native 代码:
fetch('http://justanexample.altervista.org/saveinfo.php', {
method: 'POST',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({text: "blablabla", id_product: "12"})
}).then((response) => response.text())
.then((responseData) => { console.log("response: " + responseData); })
.catch((err) => { console.log(err); });
Saveinfo.php:
echo "Hello World\n";
echo json_encode($_POST);
回复:
response: Hello World
[]
推荐答案
在你的成功处理程序中,你必须编辑 response.text() 因为你得到一个 json 响应
In your success handler you have to edit response.text() beacause you're getting a json response
fetch('http://justanexample.altervista.org/saveinfo.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({text: "blablabla", id_product: "12"})
})
.then((response) => JSON.stringify(response.json()))
.then((responseData) => { console.log("response: " + responseData); })
.catch((err) => { console.log(err); });
在你的 php 代码中使用这个而不是你现有的代码
In your php code use this instead of your existing code
$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE)
echo $obj;
表单数据示例:
var data = new FormData()
data.append({text: "blablabla", id_product: "12"});
body: data
这篇关于如何通过POST方法发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!