我只需要使用Ajax(在视图home.blade.php中)将一个值从视图传递给控制器。所有的解决方案都是针对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;
}
我收到错误404POST.../INSERT2Not found.Have你有什么想法,好吗?
发布于 2020-05-06 23:34:35
Route::post('/insert2/{datee}', 'EditContdroller@show1');
url:"insert2",
您的post路由需要额外的参数,但您的请求没有参数
它应该是url:“插入2/某物”
现在在Controller中,你传递的"something“将变成变量{datee}
如果你想让这个参数成为可选的,你应该在{datee}上加上问号{datee?},然后你的AJAX请求就可以工作了:(我加了'?‘日期的问号)
Route::post('/insert2/{datee?}', 'EditContdroller@show1');
您正在通过:
data: datee,
它不是这样工作的。
为了传递日期,我建议这样做:
删除{datee}部分
Route::post('/insert2', 'EditContdroller@show1');
在AJAX请求中,像这样修改数据:
data: { datee: datee },
在您的控制器访问日期值中,如下所示:
$datee = $request->input('datee');
https://stackoverflow.com/questions/61639028
复制相似问题