【问题标题】:$http PATCH method not sending data server side$http PATCH 方法不发送数据服务器端
【发布时间】:2017-04-15 20:13:26
【问题描述】:

我是 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 请求出现问题,我在服务器端接收到空数组。请帮我解决我的问题。

【问题讨论】:

  • 能否给我们看看你的controller\action 并提供路由注册的代码
  • 我只是使用带有资源Route::resource('people','PeopleController');的路由

标签: jquery angularjs json laravel patch


【解决方案1】:

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

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

 $scope.save = function (modalstate, id) {
        var url = $scope.url,
            data = $scope.formdata;

        //append id to the URL if the form is in edit mode
        if (modalstate === 'edit') {
            url += "/" + id;
            data = angular.merge(data, {_method: 'PATCH'});
        }

        if (!$("#myform").isValid()) {
            return false;
        } else {
            console.log('request');
            $http({
                method: "POST",
                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');
            });
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多