Goendg
Goendg

Reputation: 130

How to Handle Data Posted From Angular in Laravel

I am trying to update my database with data posted by AgularJs to Laravel, and I can't get laravel to handle data.

Route

Route::get('/api/editsubpages/', 'NewSubPagesController@editsubpage');

Laravel Controller

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

Angular Controller

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

The JSON posted in "Request Payload," but I can't even print it.

Upvotes: 0

Views: 374

Answers (1)

Goendg
Goendg

Reputation: 130

i've changed

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

To

$http.post('/api/editsubpages', {
            data: $scope.data,
        }).then(function mySuccess(response) {

        }, function myError(response) {
            console.log("error");
        });

and it's working now! can someone tell me what's the difference? i don't get it!

Upvotes: 0

Related Questions