ajax - 十月 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;但我可以在控制台中看到空响应。任何想法?
解决方案
我 return $cod_charge;
改为 return array('cod'=>$cod_charge);
. 意思是返回类型应该是数组。希望这会帮助其他人解决这种情况。
推荐阅读
- python - TypeError:当我跨进程共享我的类对象时,无法腌制 _thread.lock 对象
- watermark - 从命令行创建 pdftk 水印文件
- bash - 列出目录中包含给定名称的文件
- angular-cli - ng build 之后组件样式去哪了?
- jquery - 当我们使用 jquery 获取值时,bootstrap selectpicker 没有在数组中添加最后一个选定的项目
- ios - 应用商店连接,缺少应用商店服务器通知 URL 的输入
- unit-testing - 当我测试我的 quarkus 资源端点时,我的主要服务被调用
- ubuntu - MiniKF 安装错误,在 Kubeflow 安装过程中 94% 后失败
- mongodb - 无法使用 Moongoos 连接到 Mongodb
- excel - 无法使用 VBA 更改单元格日期格式