laravel angular添加帖子500错误

时间:2017-11-13 23:09:29

标签: php angular laravel

我正在尝试使用angular提交帖子,我正在关注此tutorial,我收到以下错误。

  

$ http.post(...)。success is not a function无法加载资源:   服务器响应状态为500(内部服务器错误)

     

$ http.post(...)。成功不是一个功能       在b。$ scope.addPost(main.js:18)

这是路线:

Route::post('auth/post', 'PostController@storePost')->name('add.post');

PostController中

public function storePost(Request $request)
{
    $data = request()->validate([
     'title' => 'required|max:120',
     'body' => 'required|max:1000'
    ]);

    $data['user_id'] = auth()->user()->id;

    $post = Post::create($data);


    // return redirect('/home')->withMessage('A new post was created.');

    return Response::json(array('success' => true));
}

Main.js

app.controller('mainCtrl', ['$scope', '$http', function($scope, $http){
    $scope.posts = {};

    $scope.addPost = function(){
        $http.post('/auth/post', {
            title: $scope.mytitle,
            body: $scope.mybody

        }).success(function(data, status, headers, config){
            $scope.posts.push(data);
            $scope.post = '';


        });

    };
}]);

1 个答案:

答案 0 :(得分:0)

我改变了成功,然后才有效。

app.controller('mainCtrl', ['$scope', '$http', function($scope, $http){
    $scope.posts = {};

    $scope.addPost = function(){
        $http.post('/auth/post', {
            title: $scope.mytitle,
            body: $scope.mybody

        }).then(function(data, status, headers, config){
            $scope.posts.push(data);
            $scope.post = '';


        });

    };
}]);