$ http PATCH方法不发送数据服务器端

时间:2016-12-01 09:06:19

标签: jquery angularjs json laravel patch

我是angularjs的新手使用Laravelangularjs制作一个crud我正在使用一个将用于更新和保存我的功能代码的函数

 $scope.save = function (modalstate, id) {
        var url = $scope.url;
        var method = "POST";
        console.log(modalstate);
        //append id to the URL if the form is in edit mode
        if (modalstate === 'edit') {
            url += "/" + id;
            method = 'PATCH';
        }
        var data = {};
        data = $scope.formdata;

        if (!$("#myform").isValid()) {
            return false;
        } else {
            console.log('request');
            $http({
                method: method,
                url: url,
                data: data,
                headers: {'Content-Type': undefined},
                transformRequest: function (data) {
                    console.log(angular.toJson(data));
                    var formData = new FormData();
                    formData.append("data", [angular.toJson(data)]);

                    if (typeof (data.logo) != "undefined")
                    {
                        formData.append("file", data.logo);
                    }
                    console.log(formData);
                    return formData;
                },
            }).success(function (response) {
                console.log(response);
                //  location.reload();
            }).error(function (response) {
                console.log(response);
                alert('This is embarassing. An error has occured. Please check the log for details');
            });
        }

当我存储记录时,上面的功能正常工作。但是在编辑更新记录时我正在使用PATCH请求,但现在使用PATCH请求我在服务器端接收空数组时出现问题。请帮我解决我的问题。

1 个答案:

答案 0 :(得分:1)

Laravel使用method spoofing为RESTful接口提供更好的支持,例如您尝试使用Route::resource构建的接口。这意味着您不使用方法PATCH,而是使用POST方法,并将值_method的{​​{1}}密钥与您的请求数据一起传递。

所以你的代码可能如下所示:

PATCH