1

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 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your answer. But it still refuses to work. I can see the data being received by the PHP as request payload in the headers section of network.
I'm sorry, I was wrong. I updated my answer to a new solution. Can you try it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.