【发布时间】:2021-06-12 21:06:38
【问题描述】:
路线:
Route::get('/user/{id}', "UserController@get")->name('user');//Get all users is not allowed.
表格:
<form>
<select id="selected_user">
<option value="1">Bob</option>
<option value="2">Alice</option>
</select>
<button id="get_user">Query</button>
</form>
AJAX:
$.ajax({
url: "{{ route('user')}}"+id,
type: "GET",
data: null,
dataType: 'json':
}).done(function(response){
console.log(response);
});
你会得到什么: development.ERROR: Missing required parameters for [Route: user] [URI: user/{id}] [Missing parameters: id].
是的,但我仍然不知道我会选择哪个用户,所以我不能在那里设置参数......
解决方法 1:
POST Request (But I am not POST ing anything?)
AJAX 中的解决方法 2:
let id = getHTMLId();
url: {{ URL::to('/'); }}+"/user/"+id;//I will have to update all AJAX requests if there is a change to URL so it is now: users/get/{id}
解决方法 3(我不知道这是否可能):
Route::get('/user/{id?}', "UserController@get")->name('user');//Optional parameters, but the application should not be allowing a request to /users/ alone.
来自 Laravel 的预期方法
URL::routeWithoutParams('user');//其中user是命名路由
有没有合适的方法来做一个简单的 GET 请求?
我没有找到任何信息,所有示例似乎都读取了整个数据,例如 users/getall
【问题讨论】: