我只需要使用 Ajax(在 View home.blade.php 中)将一个值从 View 传递到 Controller 。 所有解决方案均适用于 Laravel 5,没有帮助。
来自 home.blade.php 的 Ajax:
$datee="hello";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type : 'POST',
url : "insert2",
contentType: "text",
data: datee ,
dataType: 'json'
});
路线:
Route::post('/insert2/{datee}', 'EditContdroller@show1');
Route::get('/home', 'HomeController@index')->name('home');
还有EditController的方法:
public function show1(Request $request){
$data= $request->datee;
//some work with $data...
return $json_Value;
}
我收到错误 404 Post .../insert2 not found。请问您有什么想法吗?
最佳答案
Route::post('/insert2/{datee}', 'EditContdroller@show1');
url:"insert2",
您的发布路由需要额外参数,但您请求不带参数
应该是 url: "insert2/something"
现在在 Controller 中,您传递的“某物”将成为变量 {datee}
如果您想让参数可选,您应该向 {datee} 添加问号,使其成为 {datee?} 然后您的 AJAX 请求应该可以工作:(我在日期中添加了“?”问号)
Route::post('/insert2/{datee?}', 'EditContdroller@show1');
您正在通过:
data: datee,
它不是这样工作的。
要传递日期对象,我建议这样做:
删除 {datee} 部分
Route::post('/insert2', 'EditContdroller@show1');
在您的 AJAX 请求中修改您的数据,如下所示:
data: { datee: datee },
在您的 Controller 中访问 datee 值,如下所示:
$datee = $request->input('datee');
关于javascript - 通过 Ajax 将数据从 Laravel 7 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61639028/