首页 > 解决方案 > 十月 CMS 中的 ajax 请求总是得到空对象

问题描述

这是我的js代码:

$('body').on('change','#cash-amount-id',function(){
     var amount = $(this).val();
     calculateCODcharge(amount);
 });

function calculateCODcharge(amount){
    $('.loading-img').show();
    if(amount>0 ){
       $.request('onCalculateCODcost', {
            data: {amount: amount},
            success: function(response, status, xhr, $form) {
              console.log(response);
              $('#cod_cost_text').text(response.result);
              calculateTotalCharge();
              $('.loading-img').hide();
          },
          error: function(error){
            console.log(error);

          }
        });
    }
}

而我的 onCalculateCODcost 函数是:

function onCalculateCODcost(){
    $data = post();
    extract($data);

    $cod = \Spot\Shipment\Models\OtherSetting::where('param', 'cod')->get();

    $cod_charge = 0;
    if($cod[0]->enabled){
       $cod_charge = $amount*$cod[0]->value;
       $cod_charge /=100;
    }
    //echo $cod_charge;
    return 100;//$cod_charge;
}

为了确定这个奇怪的问题,我只返回 100;但我可以在控制台中看到空响应。任何想法?

标签: ajaxoctobercms

解决方案


return $cod_charge;改为 return array('cod'=>$cod_charge);. 意思是返回类型应该是数组。希望这会帮助其他人解决这种情况。


推荐阅读