utd*_*dev 6 javascript php ajax jquery laravel
如何使用Ajax将Javascript变量传递给Php并检索它们?
我正在使用Jquery Ui Slider,并且在每张幻灯片上我想将javascript滑块值传递给php,以便说明.
我没有(没有太多)Ajax的经验,非常感谢帮助.
这是我的滑块的样子:
$("#sliderNumCh").slider({
range: "min",
min: 0,
max: 20,
step: 1,
value: numbersOfChapters,
change : function(e, slider){
$('#sliderAppendNumCh').empty();
var i = 0;
var sliderValue = slider.value;
var getSliderVal = document.getElementById('sliderValue').value = sliderValue;
$.ajax({
type: 'POST',
url: '',
headers: {'X-Requested-With': 'XMLHttpRequest'},
data: {
value: getSliderVal
},
success: function (option) {
console.log(getSliderVal);
}
});
...
}
})
Run Code Online (Sandbox Code Playgroud)
我的路线示例:
编辑我的路线现在看起来像这样:
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProduct']);
Run Code Online (Sandbox Code Playgroud)
编辑我尝试的内容:
url: '{{ route("editProductWeb") }}',
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
Run Code Online (Sandbox Code Playgroud)POST http://localhost/myApp/public/product/edit/%7BproductID%7D 500 (Internal Server Error)
并试过这个:
url: 'edit',
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
Run Code Online (Sandbox Code Playgroud)POST http://localhost/myApp/public/product/edit 500 (Internal Server Error)
编辑我的编辑控制器方法:
public function editProduct($productRomID = 0)
{
$product = ProductRom::find($productID);
$sheets = Chapters::where('product_id', '=', $productID)->get();
$productId = $product->id;
$sheetCount = count($sheets);
return view('product.edit', [
'productId' => $productId,
'product' => $product,
'sheets' => $sheets,
'sheetCount' => $sheetCount,
'type' => 'edit',
'route' => 'updateProductRom'
]);
}
Run Code Online (Sandbox Code Playgroud)
到目前为止使用haakym建议编辑:
$.ajax({
type: 'post',
url: "{{ Route('editProduct', $product->id) }}",
headers: {'X-Requested-With': 'XMLHttpRequest'},
data: {
value: getSliderVal,
productId : getPrId
},
success: function (option) {
console.log(getSliderVal);
}
});
Run Code Online (Sandbox Code Playgroud)
会在我的调试中打印出id +当前滑块值,这样就可以了.现在我需要获得该值并在我的视图中使用它(php)任何建议如何继续?
在我的控制器方法中使用它:
$sliderValue = $request->input('value');
Run Code Online (Sandbox Code Playgroud)
回报我
空值
编辑我也试过这个:
$sliderValue = Input::get('value');
Run Code Online (Sandbox Code Playgroud)
这也归还了我
空值
编辑我添加了一个日志:
Log::info(Input::all());
Run Code Online (Sandbox Code Playgroud)
这会在幻灯片上显示正确的滑块值和产品ID.但我Input::get('value')还是会回来的null
编辑我想我应该添加这些信息:
我现在改变了我的路线:
Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProductPost']);
Run Code Online (Sandbox Code Playgroud)
该get节目从数据库中特定产品的数据,并将其显示在我看来,我加入了post一个张贴slidervalue数据的editProductPost方法,并在编辑视图后返回值(sliderValue),这是正确的?(顺便说一句仍不起作用)
编辑
如果我把它放在我的控制器方法中:
if ($request->isMethod('post')){
return response()->json(['response' => 'This is post method']);
}
return response()->json(['response' => 'This is get method']);
Run Code Online (Sandbox Code Playgroud)
我一直收到以下错误(如果我滑动):
POST http:// localhost/myApp/public/product/edit/54 500(内部服务器错误)
我脑子里有这个:
<meta name="csrf-token" content="{{ csrf_token() }}">
Run Code Online (Sandbox Code Playgroud)
把它放在我的ajax帖子之前:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:
这样做会在日志中返回正确的当前滑块值:
Log::info($request->get('value'));
Run Code Online (Sandbox Code Playgroud)
我试过这个:
return view('productRom.edit', [
'value' => $value,
]);
Run Code Online (Sandbox Code Playgroud)
但我在控制台中收到错误:
无法加载资源:服务器响应状态为500(内部服务器错误)http:// localhost/myApp/public/product/edit/73
正如 @julqas 所说,您需要在方法中包含 URL $.ajax()。
由于您有一个命名的路线editProduct,您可以使用blade输出链接:
$.ajax({
type: 'POST',
url: '{{ route("editProduct" }}',
...
Run Code Online (Sandbox Code Playgroud)
编辑1:
你的路线是get,你的ajax方法是post,我想这就是问题所在。你需要使它们相同。
如果您将路由更改为,post则需要在发送 ajax 请求时将 CSRF 令牌添加到该请求中。这里有一些关于如何执行此操作的文档指南:
https://laravel.com/docs/5.2/routing#csrf-x-csrf-token
文档建议将其添加到 HTML 头中:
<meta name="csrf-token" content="{{ csrf_token() }}">
Run Code Online (Sandbox Code Playgroud)
然后在发送请求之前使用以下代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Run Code Online (Sandbox Code Playgroud)
或者,您可以将其添加到 ajax 调用中的请求中。
编辑2
一个侧面 - 我只是猜测错误是什么,如果我问你是否可以自己调试它以查看错误是什么,那就更好了。学习调试ajax请求非常有用,而且也不是太难。
最好的方法是在发出 ajax 请求时使用您选择的浏览器中的开发人员控制台。例如,如果您使用 Chrome,请在提出请求之前打开Developer tools并单击该选项卡。Network提出请求后,您可以检查请求及其详细信息。希望有帮助!
编辑3
我会更改您的editProduct()方法以不接受任何参数,而是id从请求中获取产品的值
public function editProduct()
{
// value here is referring to the key "value" in the ajax request
$product = ProductRom::find(\Request::get('value');
...
}
Run Code Online (Sandbox Code Playgroud)
考虑将valuejson 中的键更改为更有用的内容,例如 ProductId
data: {
productId: getSliderVal
}
Run Code Online (Sandbox Code Playgroud)