如何从 angular 前端向 laravel API 发送请求?
How to send a request from angular front end to laravel API?
我正在用 Laravel 5.2 构建一个 RESTful API,我有一个 AngularJS 1.5 前端。我正在成功地编写服务来获取信息,但是当我将它传递给 API 时,我无法将任何内容放入或发布到数据库中。我已经尝试进行一些搜索,但我只是不明白如何实际保存我将发送 API 的数据。到目前为止,这是我的尝试:
-工厂服务-
addReceipt: function(request) {
return $http.post(api_url + "/rewards/receipts/add", request).then(function(results) {
console.log(results);
return results.data;
});
}
-来自控制器
$scope.submitReceipt = function() {
rewardsFactory.addReceipt($scope.model).then(function() {
console.log($scope.model);
toaster.pop({ type: 'success', title: 'Claim Submitted!', body: "Thanks! We'll take a look at your claim shortly.", showCloseButton: true });
});
};
-从LaravelAPI路线
Route::post('rewards/receipts/add', 'Rewards\RewardsController@addReceipt');
-来自 Laravel 控制器
public function addReceipt(Request $request)
{
//Add the Receipt
DB::table('receipts')->insert(
['transaction_id' => $request->input('transactionID'),
'client_id' => $request->input('client_id'),
'location_id' => $request->input('location_id') ]
);
}
我当前的 Cors 设置似乎对至少一些流量运行良好,所以我不认为这是问题所在,但我仍然不确定我做错了什么。
注意$http
默认不发送表单编码数据,它在请求正文中发送application/json
。
我没有用 laravel 做任何工作,但是如果你检查 $_POST
你会看到它是空的,所以 $request->input
可能也是空的。
在 php 中,您可以使用以下方式访问响应正文:
json_decode(file_get_contents('php://input')[,true/*optional to convert to array*/])
我相信 json_decode($request->getContent())
会在 laravel
中做同样的事情
另一种方法是使用从文档中获取的以下$http
设置来发送表单编码数据
.controller(function($http, $httpParamSerializerJQLike) {
//...
$http({
url: myUrl,
method: 'POST',
data: $httpParamSerializerJQLike(myData),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
});
您还可以在 运行 块中设置 $http.defaults
,这样所有 post 或 put 都作为 x-www-form-urlencoded
发送,而不必为每次使用添加配置
我正在用 Laravel 5.2 构建一个 RESTful API,我有一个 AngularJS 1.5 前端。我正在成功地编写服务来获取信息,但是当我将它传递给 API 时,我无法将任何内容放入或发布到数据库中。我已经尝试进行一些搜索,但我只是不明白如何实际保存我将发送 API 的数据。到目前为止,这是我的尝试:
-工厂服务-
addReceipt: function(request) {
return $http.post(api_url + "/rewards/receipts/add", request).then(function(results) {
console.log(results);
return results.data;
});
}
-来自控制器
$scope.submitReceipt = function() {
rewardsFactory.addReceipt($scope.model).then(function() {
console.log($scope.model);
toaster.pop({ type: 'success', title: 'Claim Submitted!', body: "Thanks! We'll take a look at your claim shortly.", showCloseButton: true });
});
};
-从LaravelAPI路线
Route::post('rewards/receipts/add', 'Rewards\RewardsController@addReceipt');
-来自 Laravel 控制器
public function addReceipt(Request $request)
{
//Add the Receipt
DB::table('receipts')->insert(
['transaction_id' => $request->input('transactionID'),
'client_id' => $request->input('client_id'),
'location_id' => $request->input('location_id') ]
);
}
我当前的 Cors 设置似乎对至少一些流量运行良好,所以我不认为这是问题所在,但我仍然不确定我做错了什么。
注意$http
默认不发送表单编码数据,它在请求正文中发送application/json
。
我没有用 laravel 做任何工作,但是如果你检查 $_POST
你会看到它是空的,所以 $request->input
可能也是空的。
在 php 中,您可以使用以下方式访问响应正文:
json_decode(file_get_contents('php://input')[,true/*optional to convert to array*/])
我相信 json_decode($request->getContent())
会在 laravel
另一种方法是使用从文档中获取的以下$http
设置来发送表单编码数据
.controller(function($http, $httpParamSerializerJQLike) {
//...
$http({
url: myUrl,
method: 'POST',
data: $httpParamSerializerJQLike(myData),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
});
您还可以在 运行 块中设置 $http.defaults
,这样所有 post 或 put 都作为 x-www-form-urlencoded
发送,而不必为每次使用添加配置