PHP does not execute function called via Axios in Vue.JS

一世执手 提交于 2020-02-06 03:45:12

问题


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.

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 interpretation code:

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

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


回答1:


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)


来源:https://stackoverflow.com/questions/59298868/php-does-not-execute-function-called-via-axios-in-vue-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!