首页 > 解决方案 > 如何在 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 在“请求有效负载”中发布,但我什至无法打印它。

标签: angularjsjsonlaravel

解决方案


我变了

    $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");
        });

它现在正在工作!有人可以告诉我有什么区别吗?我不明白!


推荐阅读