簡體   English   中英

PHP 不執行在 Vue.JS 中通過 Axios 調用的函數

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

我正在嘗試編寫一個 Vue.JS 模塊來進行一些數據處理並將變量發送到單獨文件中的 PHP 函數。 我正在使用 Axios 將參數解析為 PHP。 現在的問題是,Axios 請求到達了 PHP 文件,但 PHP 並沒有真正調用想要調用的函數。

來自 Vue 應用程序的 PHP 調用:

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 上調用了相同的函數。

使用 axios 發送的數據不會放入 PHP 的$_POST 相反,它位於請求正文中,並且很可能是 json 格式。 要獲得它,請嘗試以下代碼:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM