我如何使用 Ajax 将 Javascript 变量传递给 Php 并检索这些变量?
我正在使用 Jquery Ui Slider,并且在每张幻灯片上我都想将 javascript slider 值传递给 php。
我没有(不多)Ajax 经验,非常感谢帮助。
这是我的 slider 的样子:
$("#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);
}
});
...
}
})
我的路线示例:
编辑我的路线现在看起来像这样:
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProduct']);
编辑我尝试过的内容:
url: '{{ route("editProductWeb") }}',
得到这个错误:
POST http://localhost/myApp/public/product/edit/%7BproductID%7D 500 (Internal Server Error)
并尝试了这个:
url: 'edit',
得到这个错误:
POST http://localhost/myApp/public/product/edit 500 (Internal Server Error)
编辑我的编辑 Controller 方法:
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'
]);
}
到目前为止,编辑使用 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);
}
});
确实在我的调试中向我打印了 id + 当前 slider 值,所以到目前为止它仍然有效。现在我需要获取该值并在我的 View (php) 中使用它,有任何关于如何进行的建议吗?
在我的 Controller 方法中使用它:
$sliderValue = $request->input('value');
返回我
null
编辑我也试过这个:
$sliderValue = Input::get('value');
这也返回了我
null
编辑 我添加了一个日志:
Log::info(Input::all());
这会在幻灯片上显示正确的 slider 值和产品 ID。
但是我的 Input::get('value')
仍然返回我 null
编辑 我想我应该添加以下信息:
我现在改变了我的路线:
Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProductPost']);
get
显示特定产品数据库中的数据并在我的 View 中显示它们,我添加了 post
将 slider 值数据发布到 editProductPost
方法,然后在编辑 View 中返回值(sliderValue),这是正确的吗?(顺便说一句仍然不起作用)
编辑
如果我把它放在我的 Controller 方法中:
if ($request->isMethod('post')){
return response()->json(['response' => 'This is post method']);
}
return response()->json(['response' => 'This is get method']);
我不断收到以下错误(如果我滑动):
POST http://localhost/myApp/public/product/edit/54 500 (Internal Server Error)
我脑子里有这个:
<meta name="csrf-token" content="{{ csrf_token() }}">
并将其放在我的 ajax 帖子之前:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
编辑:
这样做会在日志中返回正确的当前 slider 值:
Log::info($request->get('value'));
我试过这个:
return view('productRom.edit', [
'value' => $value,
]);
但是我在控制台中得到一个错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost/myApp/public/product/edit/73
最佳答案
正如@julqas 所述,您需要将 URL 包含在您的 $.ajax()
方法中。
因为你有一个命名路由 editProduct
,你可以使用 blade 输出链接:
$.ajax({
type: 'POST',
url: '{{ route("editProduct" }}',
...
编辑 1:
你的路由是 get
而你的 ajax 方法是 post
,我想这就是问题所在。您需要使它们相同。
如果您将路由更改为 post
,您需要在发送 ajax 请求时将 CSRF token 添加到它。在文档中有一些关于如何在此处执行此操作的指导:
https://laravel.com/docs/5.2/routing#csrf-x-csrf-token
文档建议将此添加到您的 HTML 头部:
<meta name="csrf-token" content="{{ csrf_token() }}">
然后在发送请求之前使用以下代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
或者,您可以将其添加到 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');
...
}
考虑将 json 中的 value
键更改为更有用的内容,例如 productId
data: {
productId: getSliderVal
}
关于javascript - Laravel 使用 AJAX 将 Javascript 变量传递给 PHP 并检索它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436014/