我使用的是 Laravel 5.1,请求如何接受 Angular 通过 api 调用传递的 id
?这是我的代码。
angular.module('MyApp')
.factory('Account', function($http){
return {
getProductToEdit: function(pid){
//console.log(pid) ----> return 16
return $http.get('/api/productedit',pid);
}
}
});
我支持的功能:
public function updateProductAPI(Request $request)
{
$prodId = $request->all(); ---> return []
// $request->input('pid'); ---> Object {}
return response()->json($prodId);
}
谢谢!!
最佳答案
传递到 $http.get
函数的第二个对象是一个配置对象。您需要在此对象上设置 params 属性才能通过请求发送数据。
$http.get('url', { params : { pid : pid } });
You can read more about the config object here.
现在在您的 Controller 中
public function updateProductAPI(Request $request)
{
// Get the id
$id = $request->input('pid');
});
将 API 组合在一起的另一种方法是利用路由参数来处理唯一标识符,例如 ids 或 slugs。
// The get call
$http.get('url/'+pid).then(function(data) { ... });
// routes.php
Route::get('url/{pid}', 'ProductController@updateProductAPI');
// Controller method
public function updateProductAPI($id, Request $request)
{
// $id available here
}
关于javascript - Angular 将参数传递给 Laravel 进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964992/