【发布时间】:2016-11-21 00:27:59
【问题描述】:
如何使用 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);
}
});
...
}
})
我的路线示例:
编辑我的路线现在看起来像这样:
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)
编辑我的编辑控制器方法:
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 + 当前滑块值,所以到目前为止有效。现在我需要获取该值并在我的视图中使用它(php)任何建议如何继续?
在我的控制器方法中使用它:
$sliderValue = $request->input('value');
还给我
空
编辑我也试过这个:
$sliderValue = Input::get('value');
这也让我回来了
空
编辑我添加了一个日志:
Log::info(Input::all());
这会在幻灯片上显示正确的滑块值和产品 ID。
但是我的Input::get('value') 仍然返回给我null
编辑我想我应该添加以下信息:
我现在将路线更改为:
Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProductPost']);
get 显示特定产品的数据库中的数据并显示在我的视图中,我添加了 post 一个以将滑块值数据发布到 editProductPost 方法并随后返回值(滑块值)在编辑视图中,这是正确的吗?(顺便说一句仍然不起作用)
编辑
如果我把它放在我的控制器方法中:
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(内部 服务器错误)
我有这个想法:
<meta name="csrf-token" content="{{ csrf_token() }}">
并将其放在我的 ajax 帖子之前:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
编辑:
这样做会在日志中返回正确的当前滑块值:
Log::info($request->get('value'));
我试过这个:
return view('productRom.edit', [
'value' => $value,
]);
但我在控制台中收到错误:
加载资源失败:服务器响应状态为 500 (内部服务器错误)http://localhost/myApp/public/product/edit/73
【问题讨论】:
-
从这里看起来不错。您只需将控制器/方法的 URL 放入您的 $.ajax 'url' 参数中,然后它就可以工作了
-
ty,所以我必须在
url中添加edit我在问题中发布了上面的路线 -
你可以试试 url: '/edit',如果它不起作用,试试完整的 URL :)
-
/edit 是绝对路径对吗?他应该只使用编辑。
-
我收到此错误消息
POST http://localhost/myApp/public/product/edit 500 (Internal Server Error)
标签: javascript php jquery ajax laravel