你怎么和角$资源发送的X WWW的形式urlen codeD数据? [英] How do you send x-www-form-urlencoded data with Angular $resource?

查看:405
本文介绍了你怎么和角$资源发送的X WWW的形式urlen codeD数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提交 POST 的要求,只接受在 X WWW的形式,urlen $ C $的数据的服务器CD 格式。

I am trying to submit a POST request to a server which only accepts the data in the x-www-form-urlencoded format.

当我测试它的邮差,它的工作原理。例如,preVIEW标题是这样的:

When I test it out on Postman, it works. For example, the preview header looks like:

    POST /api/signin HTTP/1.1
    Host: myproj.herokuapp.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded

    email=joe%40gmail.com&password=1234567

然而,当我从我的应用程序,头,因为在Chrome浏览控制台运行它,是这样的:

However, when I run it from my app, the header, as viewed in Chrome console, looks like:

    Remote Address:10.10.10.250:80
    Request URL:http://myproj.herokuapp.com/api/signIn
    Request Method:POST
    Status Code:400 Bad Request
    Request Headersview source
    Accept:application/json, text/plain, */*
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    Connection:keep-alive
    Content-Length:53
    Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    Host:rapidit.herokuapp.com
    Origin:http://localhost
    Referer:http://localhost/rapid/v3/src/app/index/
    User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
    Form Dataview parsed
    {"email":"joe@gmail.com","password":"1234567"}

显然,这不是在正确的格式发送数据。

Clearly, it's not sending the data in the right format.

这是什么样子在我的角度厂(硬codeD登录数据):

This is what it looks like in my Angular factory (with hardcoded login data):

var LoginResource = $resource("http://myproj.herokuapp.com/api/signIn", {}, {
         post: {
            method: "POST",
            isArray: false,
            headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        }
    });
    var loginUser = function (){
        return LoginResource.post( 
            {
                email: "joe@gmail.com", 
                password: "1234567" 
            }
            ).$promise; //this promise will be fulfilled when the response is retrieved for this call
    };
    return loginUser;

如何,我可以将数据到邮政所要求的格式?

推荐答案

在默认情况下角使用应用程序/ JSON,这是不够的,设置页眉形成URL连接code,你有实际转换数据,您可以通过在$资源服务去transformRequest选项做到这一点。这是怎么样子的。

By default Angular uses application/json and it's not enough to set the header to form url encode, you have to actually transform the data, you can do that by using de transformRequest option on the $resource service. This is how it looks like.

angular.module("AuthModule")
.factory("authResource", ['$resource', 'appSettings', function ($resource, appSettings) {

    return {
        login: $resource(appSettings.serverPath + '/Token', null,
           {
               loginUser: {
                   method: 'POST',
                   headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                   transformRequest: function (data, headersGetter) {
                       var str = [];
                       for (var d in data)
                           str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                       return str.join("&");
                   }
               },
           })
    }

}]);

P / D:我没有写这一点。这code是从pluralsight的过程中所采取,被称为角从前到后使用Web API。

P/D: I didn't write this. This code was taken from a pluralsight's course, called Angular Front to Back with Web API.

这篇关于你怎么和角$资源发送的X WWW的形式urlen codeD数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆