如何使用ajax请求WHMCS挂钩函数并获取所需的返回数据?

问题描述 投票:0回答:1

我阅读了用于开发配置模块的WHMCS演示代码:

前端代码overview.tpl

后端代码provisioningmodule.php

但是我的代码后面是我想使用ajax进行请求的上半部分,因此整个页面将不会重新呈现。


我的前端ajax代码:

    jQuery.ajax({
        url: url,
        type: "POST",
        data: {
            ...
            qn_action: "bmc"

        },
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data, textStatus){

            console.log('ldl:',data)  // pay attention I want to console.log the return data. I wish it be the PHP return data's `templateVariables`.
            jQuery('.powercontrol').removeClass('disabled');
            jQuery(i_id).css('display', 'none');

            jQuery('#powerstatus-spinner').css('display', 'none'); // Status loading
        },
        error: function(jqXHR, e) {
            var msg = '';
            if(jqXHR.status==0){
                msg = 'You are offline!!\n Please Check Your Network.';
            }else if(jqXHR.status==404){
                msg = 'Requested URL not found.';
            }else if(jqXHR.status==500){
                msg = 'Internal Server Error.<br/>'+jqXHR.responseText;
            }else if(e=='parsererror'){
                msg = 'Error: Parsing JSON Request failed.';
            }else if(e=='timeout'){
                msg = 'Request Time out.';
            }else {
                msg = 'Unknow Error.<br/>'+x.responseText;
            }

            console.log('error: '+jqXHR.responseText);  // I wil get the error logs
            console.log('Error msg: '+msg);
        }
    })

在我的后端PHP代码中:

function qidicatedserver_ClientArea(array $params)
{

    // Determine the requested action and set service call parameters based on
    // the action.

    $qn_action = isset($_REQUEST['qn_action']) ? $_REQUEST['qn_action'] : '';

    $tblclients_id = isset($_REQUEST['tblclients_id']) ? $_REQUEST['tblclients_id'] : '';  


    $bmc_action = "";
    $server_name = "";
    $templateFile = 'templates/overview.tpl';
    $bmc_result = "";


    if($qn_action == "bmc"){

        $resp = array(
            'tabOverviewReplacementTemplate' => $templateFile,
            'templateVariables' => array(
                "status"=>200,
                "data"=>"my test return data"
            )
        );



        return $resp;


    }


    try {
        // Call the service's function based on the request action, using the
        // values provided by WHMCS in `$params`.


        return array(
            'tabOverviewReplacementTemplate' => $templateFile,
            'templateVariables' => array(
                'data' => array(
                    "status"=>"200",
                    "data" => array(
                        "bmc_result" => null
                    )
                ),
            ),
        );


    } catch (Exception $e) {

        //echo $e;
        // Record the error in WHMCS's module log.
        logModuleCall(
            'qidedicatedserver',
            __FUNCTION__,
            $params,
            $e->getMessage(),
            $e->getTraceAsString()
        );
        // In an error condition, display an error page.
        return array(
            'tabOverviewReplacementTemplate' => 'templates/error.tpl',
            'templateVariables' => array(
                'usefulErrorHelper' => $e->getMessage(),
            ),
        );

    }

}

当执行ajax请求时,将出现错误,请执行以下代码:

console.log('error: '+jqXHR.responseText);  // I wil get the console log: `error: <!DOCTYPE html> \n<html lang="en">\n<head>\n    <meta char) ....` the html is the whole page's HTML
console.log('Error msg: '+msg);  // the console log: `Error msg: Error: Parsing JSON Request failed.`

所以,那里肯定有问题,我的问题是如何在WHMCS自定义配置模块中对HTTP请求使用ajax,但是我尝试像上一样失败。 (我尝试使用表单请求PHP,可以获取正确的数据,但是会刷新整个页面,这不是我想要的,我不刷新页面)

谁能告诉我如何使用Ajax请求WHMCS钩子函数并按我的意愿返回正确的数据?


您可以找到WHMCS供应模块there的更多介绍。


EDIT-01

我尝试更改返回的PHP代码,如下所示:

if($qn_action == "bmc"){

    $resp = array(
        'tabOverviewReplacementTemplate' => $templateFile,
        'templateVariables' => json_encode(array(
            "status"=>200,
            "data"=>"dasdas"
        ))
    );



    return $resp;


}

但仍然是此错误。

javascript php ajax hook whmcs
1个回答
0
投票

将您的Ajax / PHP代码放在单独的文件中,而不是在_ ClientArea方法中,假设您的模块名为'mail_service',然后在以下位置创建文件:

\模块\服务器\ mail_service \ for_json.php

for_json.php

<?php
#define("ADMINAREA", true);
define("CLIENTAREA", true);
#define("SHOPPING_CART", true);
require_once(__DIR__ . '/../../../init.php');
require_once(ROOTDIR . '/includes/dbfunctions.php');
#require(ROOTDIR . "/includes/orderfunctions.php");
#require(ROOTDIR . "/includes/domainfunctions.php");
#require(ROOTDIR . "/includes/configoptionsfunctions.php");
#require(ROOTDIR . "/includes/customfieldfunctions.php");
#require(ROOTDIR . "/includes/clientfunctions.php");
#require(ROOTDIR . "/includes/invoicefunctions.php");
#require(ROOTDIR . "/includes/processinvoices.php");
#require(ROOTDIR . "/includes/gatewayfunctions.php");
#require(ROOTDIR . "/includes/modulefunctions.php");
#require(ROOTDIR . "/includes/ccfunctions.php");
#require(ROOTDIR . "/includes/cartfunctions.php");
#include_once(ROOTDIR . '/includes/clientareafunctions.php');

#use WHMCS\ClientArea;
#use WHMCS\Database\Capsule;
#use WHMCS\Smarty ;
file_put_contents("C:/xampp_my/htdocs/my/sss.txt",var_export($_POST,true));
if(isset($_POST['qn_action']) && $_POST['qn_action']=="bmc")
{
    // do your job
    //header('Content-type: application/json; charset=utf-8');
    header("Content-Type: application/json", true);

    header('HTTP/1.0 200 OK');
    echo '{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"}]}}}';
}
?>

并使用下面的JS代码:

jQuery.ajax({
    url: "/modules/servers/mail_service/for_json.php",
    type: 'POST',
    data: {qn_action: 'bmc'},
    cache: false,
    dataType: 'json',
    success: function (data, textStatus){console.log('ldl:',data)},
    error: function(jqXHR, e){
        var msg = '';
        if(jqXHR.status==0){
            msg = 'You are offline!!\n Please Check Your Network.';
        }else if(jqXHR.status==404){
            msg = 'Requested URL not found.';
        }else if(jqXHR.status==500){
            msg = 'Internal Server Error.<br/>'+jqXHR.responseText;
        }else if(e=='parsererror'){
            msg = 'Error: Parsing JSON Request failed.';
        }else if(e=='timeout'){
            msg = 'Request Time out.';
        }else {
            msg = 'Unknow Error.<br/>'+x.responseText;
        }
        console.log('error: '+jqXHR.responseText);  // I wil get the error logs
        console.log('Error msg: '+msg);
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.