angularjs - 如何在 Laravel 中处理从 Angular 发布的数据
问题描述
我正在尝试使用 AgularJs 发布到 Laravel 的数据更新我的数据库,但我无法让 laravel 处理数据。
路线
Route::get('/api/editsubpages/', 'NewSubPagesController@editsubpage');
Laravel 控制器
public function editsubpage(Request $request)
{
dd(json_decode($request->getContent(), true)); // returns empty array
// here goes code for updating database
return response()->json($request->all(), 200);
}
角度控制器
$http({
method: 'POST',
url: '/api/editsubpages/' ,
contentType: 'JSON',
headers: {'Content-Type': 'application/json'},
processData: false,
data: $scope.data
}).then(
function success(response) {
console.log("error");
},
function failed(response) {
console.log("error");
});
JSON 在“请求有效负载”中发布,但我什至无法打印它。
解决方案
我变了
$http({
method: 'POST',
url: '/api/editsubpages/' ,
contentType: 'JSON',
headers: {'Content-Type': 'application/json'},
processData: false,
data: $scope.data
}).then(
function success(response) {
console.log("error");
},
function failed(response) {
console.log("error");
});
到
$http.post('/api/editsubpages', {
data: $scope.data,
}).then(function mySuccess(response) {
}, function myError(response) {
console.log("error");
});
它现在正在工作!有人可以告诉我有什么区别吗?我不明白!
推荐阅读
- batch-file - 批处理文件中 if 语句中的通配符
- spring-cloud-stream - Spring Cloud Stream 和 GCP pub sub 作为 binder 卡住
- c# - Windows 窗体:Metroframework 在 Windows 7 和 Windows 10 上看起来不同
- postgresql - Postgres 日期搜索比小于和大于更慢
- parameters - Angular 6 HTTP GET 发送一个 ID 参数
- javascript - vue.js 组件没有显示?
- c - 编程语言语法的 Bison Shift/Reduce Conflict
- oracle - 如何将游标内的记录转换为选择语句的列名?
- go - Colly 没有找到任何链接
- postgresql - PostgreSQL 将 SERIAL 列添加到现有表中,其值基于 ORDER BY