【发布时间】:2015-07-22 06:44:33
【问题描述】:
我的 json 发布请求有一个名为“id”的数据键,现在我要如何将它从路由传递到我的控制器?
我的 json 发布请求
$.post("/employees", {id:"1"}, function(response){
if(response.success)
{
var branchName = $('#branchname').empty();
$.each(response.employees, function(){
$('<option/>', {
value:$(this).user_no,
text:$(this).firstname
}).appendTo(branchName);
});
}
}, 'json');
从我的 json 发布请求中可以看到,我已经输入了一个键值名称 id
我的路线
Route::post('employees', [
'as' => 'employees', 'uses' => 'mot@getemployee'
]);
和我的控制器
public function getemployee($id){
$employees = employees::where("branch_no", $id)->lists('firstname', 'user_no');
return response()->json(['success' => true, 'employees' => $employees]);
}
如你所见,我有一个参数 $id,它应该是来自 json post 请求的名为 id 的键值将被放置并从查询内容中使用的位置。
【问题讨论】:
-
既然你使用
$.post,你应该在你的函数中使用$id = $_POST['id'];而不把它当作参数吗? -
你有脚本的实时版本吗?
-
没问题,能否将您在浏览器控制台中看到的回复添加到您的问题中?
-
看在上帝的份上,你正在使用一个框架,应该使用 Request::input('id');而不是 $_POST
标签: php jquery json laravel laravel-5