【问题标题】:Axios Post using React Native variableAxios Post 使用 React Native 变量
【发布时间】:2022-01-11 05:46:48
【问题描述】:

React Native 新手,试图弄清楚为什么我的 react native 变量没有传入我的 axios 发布请求。这有效:

    let lat = (location.latitude);
    let lon = (location.longitude);


    const loadMessages = async () => {


        let res = await axios.post('https://example.com/list.php?coords='+(lat)+','+(lon),  
        
        {
            latitude: 'foo'
        });


我在我的 php 后端得到 'foo',而且我还能够从 $_GET 访问坐标。但是,当我尝试在发布数据中添加一个变量时,它却不起作用。我正在尝试:

    let body = { latitude: lat };
     //body= JSON.stringify(body);


    const loadMessages = async () => {

        let res = await axios.post('https://example.com/list.php?coords='+(lat)+','+(lon),  
        
        {
            body
        });

在我的后端使用:

$_POST = json_decode(file_get_contents('php://input'), true);

我认为问题可能在于我在 PHP 端调用/尝试访问对象的方式。如果可能,请包括如何在 PHP 端引用访问权限。

【问题讨论】:

    标签: php react-native axios


    【解决方案1】:

    使用 axios.get 对您来说会更容易,因为您当前的有效负载不需要使用 axios.post,您可以在 PHP 端访问 $_GET 下的所有属性,如果没有工作示例,我很难看到问题

    【讨论】:

    • 不是为了即时的情况,而是为了当我需要发布数据时,出于安全、长度等各种原因。
    猜你喜欢
    • 2019-05-18
    • 2019-08-09
    • 2018-01-07
    • 2022-11-04
    • 2019-04-07
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多