【发布时间】:2016-12-15 04:08:27
【问题描述】:
我构建了一个 ajax 帖子,它将每个滑块值(我正在使用 jquery ui 滑块)发送到我的控制器。
Ajax 代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'post',
contentType: "application/json",
url: "{{ Route('editProductPost', $product->id) }}",
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
data: JSON.stringify({
value: getSliderVal,
productId : getPrId
}),
datatype: 'json',
success: function(response) {
// get response
console.log(response.sliderValue)
}
});
在我的控制器中,我正在这样做:
public function editProductPost(Request $request)
{
Log::info($request->get('value'));
return view('product.edit', [
'sliderValue' => $request->get('value')
]);
}
这会返回正确的滑块值,
Log::info($request->get('value'));
但我在浏览器控制台中收到此错误消息:
POST http://localhost/myApp/public/product/edit/98 500(内部 服务器错误)
稍后我想在我认为的 php 循环中调用这个 sliderValue。
编辑
我确实有一个 csrf 令牌:
<meta name="csrf-token" content="{{ csrf_token() }}">
编辑
我已经这样做了:
$sliderValue = $request->get('value');
$route = 'updateProduct';
return view('product.edit', compact(['sliderValue', 'route']))->render();
控制台打印我undefined,如果我这样做{{ sliderValue }},我收到一个错误,sliderValue 没有定义
【问题讨论】:
-
如果你得到 Log::info($request->get('value'));值,那么你应该返回 json 数据而不是视图。
-
@hizbul25 我之前已经这样做了,但我不能像那样在视图中使用变量(在我的 php 循环中)
-
添加
error:function(response){ console.log(response.responseText); } -
这将为您提供导致问题的错误。
-
@jaysingkar 谢谢,这在我的控制台中返回了很多 html
标签: javascript php jquery ajax laravel