对于PUT方法,不允许使用laravel角度的方法

时间:2017-01-01 13:11:08

标签: angularjs laravel put

这是我的表格

<form class="form-horizontal alert alert-info" id="editForm" ng-submit="updateComment(currentComment)" hidden>
            <h3 class="text-center">Update Comment Details</h3>
            <div class="form-group">
                <label for="Name">Author Name:</label>
                <input type="text" class="form-control" ng-model="currentComment.author" value="{{currentComment.author}}">
            </div>
            <div class="form-group">
                <label for="text">Comment Text:</label>
                <input type="text" class="form-control" ng-model="currentComment.text" value="{{currentComment.text}}">
            </div>
            <input name="_method" type="hidden" value="PUT">
            <div class="form-group">
                <button class="btn btn-warning" ng-disabled="" ng-click="">Update</button>
                <button class="btn btn-warning" ng-disabled="" ng-click="HideEditForm()">Cancel</button>
            </div>
        </form>

这是我的服务

angular.module('commentService', [])

    .factory('Comment', function($http) {

        return {
            get : function() {
                return $http.get('api/comments');
            },
            show : function(id) {
                return $http.get('api/comments/' + id);
            },
            save : function(commentData) {
                return $http({
                    method: 'POST',
                    url: 'api/comments',
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                    data: $.param(commentData)
                });
            },
            update : function(commentData) {
                return $http({
                    method: 'PUT',
                    url: 'api/comments',
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
                    data: $.param(commentData)
                });
            },
            destroy : function(id) {
                return $http.delete('api/comments/' + id);
            }
        }

    });

这是我的角度控制器

angular.module('mainCtrl', ['datatables'])

    .controller('mainController', function($scope, $http, Comment) {
        // object to hold all the data for the new comment form
        $scope.commentData = {};

        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the comments first and bind it to the $scope.comments object
        Comment.get()
            .success(function(data) {
                $scope.comments = data;
                $scope.loading = false;
            });


        // function to handle editing the form
        $scope.currentComment = {};
        $scope.editForm = function(id){
            Comment.show(id).success(function(data){
                $scope.currentComment = data;
                $('#empForm').slideUp();
                $('#editForm').slideToggle();
            });

        };

        $scope.updateComment = function(commentData){
            Comment.update(commentData).success(function(data){
            });
        };

        //hide edit form
        $scope.HideEditForm = function(){
            $('#editForm').slideToggle();
        };
        // function to handle submitting the form
        $scope.submitComment = function() {
            $scope.loading = true;

            // save the comment. pass in comment data from the form
            Comment.save($scope.commentData)
                .success(function(data) {
                    $scope.commentData = {};
                    // if successful, we'll need to refresh the comment list
                    Comment.get()
                        .success(function(getData) {
                            $scope.comments = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
        };

        // function to handle deleting a comment
        $scope.deleteComment = function(id) {
            $scope.loading = true; 

            Comment.destroy(id)
                .success(function(data) {

                    // if successful, we'll need to refresh the comment list
                    Comment.get()
                        .success(function(getData) {
                            $scope.comments = getData;
                            $scope.loading = false;
                        });

                });
        };

    });

这是我的控制器

<?php

class CommentController extends \BaseController {

    /**
     * Send back all comments as JSON
     *
     * @return Response
     */
    public function index()
    {
        return Response::json(Comment::get());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        Comment::create(array(
            'author' => Input::get('author'),
            'text' => Input::get('text')
        ));

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

    /**
     * update a resource in storage.
     *
     * @return Response
     */
    public function update()
    {
        Comment::where('id', Input::get('author'))->update(['author'=>Input::get('author'), 'text'=>Input::get('text')]);

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

    /**
     * Return the specified resource using JSON
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        return Response::json(Comment::find($id));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        Comment::destroy($id);

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

}

所有方法都有效,除了put方法我是新的角度请帮我提前解决问题

1 个答案:

答案 0 :(得分:0)

对于您的情况,您可以使用发布,但是您应该在请求中添加名为_method = PUT的新字段( commentData

enter image description here