代码之家  ›  专栏  ›  技术社区  ›  Goendg

如何在Laravel中处理从Angular发布的数据

  •  0
  • Goendg  · 技术社区  · 7 年前

    我试图用AgularJs向Laravel发布的数据更新我的数据库,但我无法让Laravel处理数据。

    路线

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

    拉威尔控制器

    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发布在“请求负载”中,但我甚至无法打印它。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Goendg    7 年前

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

    现在它开始工作了!有人能告诉我有什么不同吗?我不明白!