ajax - Laravel AJAX - 如何在 console.log 中显示错误
问题描述
我想显示错误,或者想知道为什么这段代码在错误中运行:function()
结果总是运行到错误:函数。我想运行 success:function(data) 并重新加载此页面。
但是控制台没有显示任何关于错误的信息。
====== 阿贾克斯 ======
function ex_go(r_idx)
{
if(confirm("Are you sure?") == true)
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type:'POST',
dataType: 'JSON',
url: "{{ route('change-centerYn') }}",
data:{r_idx:r_idx},
success:function(data){
alert(data.success);
location.reload();
},
error:function(xhr, data){
console.log(xhr);
},
}else{
return false;
}
}
====== 控制器 ======
public function ex_ok(Request $request)
{
if(request()->ajax())
{
$r_idx = 'Hello';
var_dump('<pre>', $r_idx);
return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
}
}
解决方案
它突然起作用了!难以置信的!非常感谢!你救了我的早晨!
使用尝试捕获
========= AJAX ===========
function ex_go(r_idx)
{
if(confirm("해당 결제건을 지원센터로 보내시겠습니까?") == true)
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
console.log(r_idx);
$.ajax({
type:'POST',
dataType: 'JSON',
url: "{{ route('change-centerYn') }}",
data:{r_idx:r_idx},
success:function(data){
alert(data.success);
location.reload();
},
error:function(xhr, data){
console.log(xhr);
}
});
}else{
return false;
}
}
========= Laravel 控制器 =======
public function ex_ok(Request $request)
{
try
{
if(request()->ajax())
{
$r_idx = $request->r_idx;
$lecture = DB::table('class_order')
->select('*')
->where('r_idx', '=', $r_idx)
->first();
if ($lecture->r_oid != '') {
$insert_data = [
'r_oid' => $lecture->r_oid,
'r_user_id' => $lecture->r_user_id,
'r_name' => $lecture->r_name,
'r_tel' => $lecture->r_tel,
'r_hp' => $lecture->r_hp,
'r_email' => $lecture->r_email,
'r_zip' => $lecture->r_zip,
'r_addr1' => $lecture->r_addr1,
'r_addr2' => $lecture->r_addr2,
'r_class' => $lecture->r_class,
'r_enddate' => $lecture->r_enddate,
'app_endday' => $lecture->app_endday,
'whole_study' => $lecture->whole_study,
];
DB::table('ex_class_order')->insert($insert_data);
ClassOrder::where('r_idx', '=', $r_idx)->update(['centerYn' => 'y']);
$info_txt = "처리되었습니다.";
}
else
{
$info_txt = "처리실패";
}
return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
}
}
catch(\Exception $e)
{
\Log::error($e); // create a log for error occurrence at storage/log/laravel.log file
return response()->json($e->getData(), $e->getStatusCode());
}
}
推荐阅读
- mysql - debezium 对源数据库有什么影响?
- c# - 在每次调用时重新评估常量 FieldExpression 的值
- sapui5 - 如何在我的自定义 sapui5 控件中获取初始上下文路径?
- python - Pandas datetime 列在达到午夜时间戳时增加日期
- node.js - 端口未在 nuxt+Vue 应用程序上打开/分配
- php - 如何解决:“无法启动会话,因为标头已由第 1 行的“php://input”发送。”
- mysql - MySQL查询执行时间的巨大差异,相同的查询,相同的结果集大小
- django - django.db.utils.OperationalError:尝试写入只读数据库
- php - 为什么标签被挤在一起并且不改变颜色?
- dockerfile - 将 PHP-Apache2 从 Debian 迁移到 Alpine 时缺少目录和文件