PHP无法执行Vue.JS中通过Axios调用的函数 [英] PHP does not execute function called via Axios in Vue.JS

查看:164
本文介绍了PHP无法执行Vue.JS中通过Axios调用的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个Vue.JS模块来进行一些数据处理,并将变量发送到单独文件中的PHP函数.我正在使用Axios将参数解析为PHP.现在的问题是,Axios请求到达了PHP文件,但是PHP实际上并未调用打算调用的函数.

I am trying to write a Vue.JS module to do some data processing and send variables to a PHP functions in a seperate file. I'm using Axios to parse parameters to PHP. Now the problem is, the Axios request reaches the PHP file, but the PHP doesn't actually call the function that was intented to call.

从Vue应用程序调用PHP:

PHP call from Vue app:

axios.post('./crmFunctions.php',{ params: { function2call:"sendQuery", id:1, mid:1, message: "Hello there!", event:"Query" }})
.then(response => this.responseData = response.data)
.catch(error => {});

PHP解释代码:

if(isset($_POST['function2call']) && !empty($_POST['function2call'])) {
    if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }{
    $function2call = $_POST['function2call'];

    switch($function2call) {

    case 'sendQuery' : sendQuery($_POST['arguments'][0],$_POST['arguments'][1],$_POST['arguments'][2],$_POST['arguments'][3]);
    break;
    case 'other' : // do something;break;
        // other cases
    }
}
}

此代码在哪里出问题?我以前设法在AJAX上调用了相同的功能.

Where am I going wrong with this code? I managed to call the same function on AJAX previously.

推荐答案

用axios发送的数据未放入PHP的$_POST中. 相反,它位于请求正文中,并且很有可能采用json格式. 要获取它,请尝试以下代码:

The data that is send with axios is not put into PHP's $_POST. Instead, it is in the request body and most likely in json format. To get it, try the following code:

function getRequestDataBody()
{
    $body = file_get_contents('php://input');

    if (empty($body)) {
        return [];
    }

    // Parse json body and notify when error occurs
    $data = json_decode($body, true);
    if (json_last_error()) {
        trigger_error(json_last_error_msg());
        return [];
    }

    return $data;
}


$data = getRequestDataBody();
var_dump($data)

这篇关于PHP无法执行Vue.JS中通过Axios调用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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