Ajax Laravel返回视图500错误 [英] Ajax Laravel return view 500 Error
问题描述
我将幻灯片中的每个滑块值(Jquery Ui Slider)通过Ajax传递给我的控制器。
I am getting each slider value in slide (Jquery Ui Slider) via Ajax to my Controller.
Slider + Ajax看起来像这样:
The Slider + Ajax looks like this:
$("#sliderNumCh").slider({
range: "min",
min: 0,
max: 20,
step: 1,
value: numbersOfChapters,
change : function(e, slider){
$('#sliderAppendNumCh').empty();
var sliderValue = slider.value;
var getSliderVal = document.getElementById('sliderValue').value = sliderValue;
var getPrId = document.getElementById('editId').value;
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'post',
url: "{{ Route('editProductPost', $product->id) }}",
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
data: {
value: getSliderVal,
productId : getPrId
},
success: function (option) {
console.log(getSliderVal);
}
});
},
});
因此,我的确有这个想法:
Thus I do have this in my head:
<meta name="csrf-token" content="{{ csrf_token() }}">
我的路线如下:
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProductPost']);
在我的Controller方法中,我称之为:
And in my Controller method I call it like this:
public function editProductPost(Request $request)
{
$sliderValue = $request->get('value');
Log::info($sliderValue);
return view('productRom.edit', [
'sliderValue' => $sliderValue
]);
}
Log :: info($ sliderValue)告诉我,我确实得到了正确的结果每张幻灯片上的滑块值。
Log::info($sliderValue) tells me that I do get the correct slider value on each slide.
当我尝试返回编辑视图时,我在控制台中收到此错误:
When I try to return back to my edit view I get this error in the console:
POST http:// localhost / myApp / public / product / edit / 73 500(内部
服务器错误)
POST http://localhost/myApp/public/product/edit/73 500 (Internal Server Error)
我该如何解决这个问题?
How can I solve this?
编辑
由于我在视图中有这一行:
Since I have this line in my view:
<form action="{{ route($route) }}"...>
路由变量未定义,所以我在return语句中添加了这个。
The route variable was not defined so I added this in my return statement.
return view('productRom.edit', [
'sliderValue' => $sliderValue,
'route' => 'editProductPost'
]);
错误消失但我尝试访问 $ sliderValue
这样的变量:
The error is gone but when I try to access the $sliderValue
variable like this:
<p>{{ isset($sliderValue) ? $sliderValue : "nothing" }}</p>
它打印我没有
修改
控制器:
public function editProductPost(Request $request)
{
return response()->json([
'sliderValue' => $request->get('value')
]);
}
查看(ajax):
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'post',
url: "{{ Route('editProductPost', $product->id) }}",
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
data: {
value: getSliderVal,
productId : getPrId
},
success: function (response) {
// check it has some data
if (response.length) {
// spit it out
console.log(response);
} else {
console.log('nothing returned');
}
}
});
推荐答案
您的控制器方法应如下所示:
Your controller method should look like this:
public function editProductPost(Request $request)
{
return response()->json([
'sliderValue' => $request->get('value')
]);
}
您不应该将视图返回给ajax请求。
You shouldn't be returning a view to a ajax request.
您的ajax成功方法应如下所示(例如):
Your ajax success method should look like this (for example):
// first arg is the data returned by the controller method
success: function (response) {
console.log(response);
}
它应该输出这样的东西:
and it should ouput something like this:
{
'sliderValue': 4
}
这篇关于Ajax Laravel返回视图500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!