简体   繁体   English

React-Native提取未将主体内容发送到$ _POST

[英]React-Native fetch not sending body content to $_POST

I'm having an issue with a POST request using React Native's fetch. 我在使用React Native的提取的POST请求时遇到问题。 The request works perfectly fine, but the $_POST variables are not appearing at all server side. 该请求工作正常,但是$ _POST变量并未出现在所有服务器端。 I've searched around quite a bit before posting, but no dice. 发布之前,我已经搜索了很多,但没有骰子。

Can anyone point me in the right direction? 谁能指出我正确的方向? The code for the fetch request is below. 提取请求的代码如下。

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);
    });
}

How I'm accessing it on PHP: 我如何在PHP上访问它:

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

I've also tried hardcoding the following: 我也尝试过硬编码以下内容:

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);
    });
}

now access the username and password 现在访问用户名和密码

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

The data does not go to _POST array if sent the way you send it. 如果以您发送的方式发送,则数据不会进入_POST数组。 You can get it on PHP side using 您可以使用PHP在PHP方面获得它

json_decode(file_get_contents('php://input'), true)

Or use FormData object and remove json headers in js to get the data traditionally (via _POST and _GET). 或使用FormData对象并删除js中的json标头以传统方式获取数据(通过_POST和_GET)。

I am using the same approach and it works for me. 我正在使用相同的方法,它对我有用。 Two things you can try 您可以尝试的两件事

  1. Remove the accept key from headers. 从标题中删除接受密钥。 i am not using this header 我没有使用此标题
  2. I noticed your data object is called data string, is it possible you are stringifying the data twice? 我注意到您的数据对象称为数据字符串,是否有可能对数据进行两次字符串化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM