简体   繁体   English

Angular $ resource服务以表单数据而不是请求有效载荷发送数据

[英]Angular $resource service send data in Form Data instead of Request Payload

I'm facing an issue with angular $resource service. 我面对有角度的$ resource服务的问题。

I'm trying to hit a REST API with some form data in different ways. 我试图以不同的方式使用某些表单数据来创建REST API。

But every time I hit the API, I either end up sending the data in QueryParams or Request Payload. 但是,每次我使用该API时,要么最终要么以QueryParams的形式发送数据,要么最终请求有效载荷。

I actually need to send the data as form encoded. 我实际上需要将数据以编码形式发送。

My code looks as below: 我的代码如下所示:

var headers = {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        };


var save = function (url, params, headers, successcb, successParams, errorcb, errorParams) {
            var promise = $resource(url, null, {
                upload: {
                    method: methodPost,
                    headers: headers
                }
            }).save({}, params).$promise;

            promise.then(function (response) {
                successcb(response.data, successParams);
            }, function (error) {
                errorcb(error, errorParams);
            });
        };

I've attached a snapshot of the angular ajax request. 我已经附上了角度ajax请求的快照。

在此处输入图片说明

Referring to the image above, the content type is actually showing application/json though I've set x-www-form-urlencoded. 参考上面的图片,尽管我设置了x-www-form-urlencoded,但内容类型实际上是显示application / json。 Hence the data is going out in RequestPayload instead of FormData. 因此,数据将在RequestPayload中而不是FormData中输出。

Below is one more image of jQuery ajax call. 下面是jQuery ajax调用的另一幅图像。

在此处输入图片说明

Here, you can clearly see that the content-type is x-www-form-urlencoded and it is working fine. 在这里,您可以清楚地看到content-type是x-www-form-urlencoded,并且工作正常。

My doubt here is, $resource is not sending out proper headers because of which the data is going in the form of RequestPayload. 我的疑问是,$ resource没有发送适当的标头,因此数据以RequestPayload的形式发送。

Is there anyone out there who has faced this issue? 有没有人遇到过这个问题?

Any help is appreciated. 任何帮助表示赞赏。

Thanks 谢谢

You can use transform request for that, like below. 您可以为此使用转换请求,如下所示。

var transform = function(data) {
        return $.param(data);
    }

    var headers = {
        'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
    };

    var save = function(url, params, headers, successcb, successParams, errorcb, errorParams) {
        var promise = $resource(url, null, {
            upload : {
                method : methodPost,
                headers : headers,
                transformRequest : transform

            }
        }).save({}, params).$promise;

        promise.then(function(response) {
            successcb(response.data, successParams);
        }, function(error) {
            errorcb(error, errorParams);
        });
    };

This is due to the transformRequest method in the $http service $resouce is dependent on which always sets the Content-Type header to application/json . 这是由于$http服务$resoucetransformRequest方法所依赖,该方法始终将Content-Type标头设置为application/json

To correct this, you need to disable it or change it to use a function which sets the specs to what you want. 要纠正此问题,您需要禁用它或将其更改为使用将规格设置为所需功能的功能。

Good article on it here: http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm 此处的好文章: http : //www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm

Try: 尝试:

var promise = $resource(url, null, {
    upload: {
        method: methodPost,
        headers: headers,
        transformRequest: []        
    },
}).save({}, params).$promise;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM