发送正文 React Native - PHP

问题描述 投票:0回答:1

我正在我的 React-native 项目中发送 clientid 变量。我在发帖时使用 post 方法发送正文。但是现在 clientid 变量不会发送到 php,它会发送一个空值。

React-Native 代码

 let rspNew = await get3dNumber({
                clientid: "500300000",
                ...
            })


export function get3dNumber(CART){
    return fetch(`http://...php`,{
        method:'POST',
        headers: {
            'Accept': 'application/json',
                             'Content-Type': 'application/x-www-form-urlencoded',
                        },
         redirect:'follow',
        body:JSON.stringify(CART)
    })
}

PHP 代码

<?php

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");
    
    $json = json_decode(file_get_contents('php://input'), true);
    
    $myArray = array();
    $myArray['clientid'] = $json['clientid']; 
    ?>
react-native
1个回答
0
投票

这就是我从 PHP API 获取数据的方式

 fetch('http://...php',{
                 method: 'POST',
                 headers :{
                     Accept: 'application/json',
                     'Content-Type' : 'application/json',
                 },
                 body: JSON.stringify({
                    key: data,
                    key2:data
                 })
             })
             .then((response) => response.json())
             .then( (responseJson)=> {
               console.log(responseJson)
             });

在 PHP 页面中

$obj   =json_decode(file_get_contents('php://input'), true);
$key = (!empty($obj['key'])) ? trim($obj['key']) : '' ;
$key2 = (!empty($obj['key2'])) ? trim($obj['key2']) : '' ;

希望这对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.