gpt4 book ai didi

javascript - React-Native 获取不将正文内容发送到 $_POST

转载 作者:行者123 更新时间:2023-11-30 15:36:43 24 4
gpt4 key购买 nike

我在使用 React Native 的 fetch 进行 POST 请求时遇到问题。该请求工作得很好,但 $_POST 变量并未出现在所有服务器端。在发布之前我已经搜索了很多,但没有找到答案。

谁能指出我正确的方向?获取请求的代码如下。

const dataString = `username=${email}&password=${password}`;

export default function ajaxPost(dataString, url, callback) {
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(dataString)
})
.then((response) => response.json())
.then((responseJson) => {
callback(responseJson);
})
.catch((error) => {
console.error(error);
});
}

我如何在 PHP 上访问它:

$data = 'username=' . $_POST['username'] . '&password=' . $_POST['password'];
echo json_encode($data);

我还尝试对以下内容进行硬编码:

export default function ajaxPost(dataString, url, callback) {
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'test',
password: 'test123'
})
})
.then((response) => response.json())
.then((responseJson) => {
callback(responseJson);
})
.catch((error) => {
console.error(error);
});
}

最佳答案

export default function ajaxPost(email,password, url, callback) {
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
user:{
username: email,
password: password,
}
})
})
.then((response) => response.json())
.then((responseJson) => {
callback(responseJson);
})
.catch((error) => {
console.error(error);
});
}

现在访问用户名和密码

$_POST['user']['username'], $_POST['user']['password']

关于javascript - React-Native 获取不将正文内容发送到 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41421392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com