撷取react native不会传送讯息 [英] Fetch in react native not send the post

查看:78
本文介绍了撷取react native不会传送讯息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将发布变量发送到我的API,但我没有在PHP文件中获取发布数据

Hello I am trying to send post variables to my API and I not getting the post data in the PHP file

这是我的反应本机代码:

This is my react native code:

let data = {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        'firstname': 'test',
    }),
}
fetch(GLOBALS.API + '/registerApi.php?key=' + GLOBALS.KEY, data)
.then((response) => response.json())
.then((responseJson) => {
    Alert.alert(
      'Alert Title',
      responseJson.output
    )

})
.catch((error) => {
    console.error(error);
});

它让我空了:[]

$array = array(
    "output" => json_encode($_POST)
);
$output = json_encode($array);
die($output);

当我使用$_REQUEST时,它仅返回key get参数,而没有firstname一个.

When I use $_REQUEST it's returning me only the key get parameter without the firstname one.

推荐答案

在PHP中,您需要使用

In the PHP, you need to use

$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE)

这篇关于撷取react native不会传送讯息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆