非常简单AngularJS $ HTTP POST中的结果“400(坏请求)”和“无效的HTTP状态code 400' [英] Very Simple AngularJS $http POST Results in '400 (Bad Request)' and 'Invalid HTTP status code 400'

查看:3761
本文介绍了非常简单AngularJS $ HTTP POST中的结果“400(坏请求)”和“无效的HTTP状态code 400'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure中托管,有两个非常简单的方法非常简单的.NET Web API:

I have a very simple .NET Web API hosted in Azure, with two very simple methods:

[EnableCors(origins: "http://simpleapiearl.azurewebsites.net", headers: "*", methods: "*")]
public class EnvelopesController : ApiController {
    // GET: api/Envelopes
    public IEnumerable<string> Get() {
        return new string[] { "value1", "value2" };
    }

    // POST: api/Envelopes
    public string Post([FromBody]Envelope env) {
        return "rval: " + env + " (and my addition to env)";
    }
}

我创建了一个简单普拉克来调用这些方法。在我AngularJS code,我正在做两个非常简单的$ HTTP调用。获取工作正常。的帖子,然而,总是返回400(错误请求),随后不久,由XMLHtt prequestion不能加载中.....无效HTTP状态code 400我WebStorm控制台。

I have created a simple plunk to call these methods. In my AngularJS code, I'm making two very simple $http calls. The GET works fine. The POST, however, always returns a "400 (Bad Request)", followed shortly in my WebStorm console by "XMLHttpRequestion cannot load ... Invalid HTTP status code 400".

任何和所有的建议AP preciated!

Any and all suggestions appreciated!

编辑!!

您好,感谢您的非常快的反应。我才意识到我忘了补充一些非常相关的细节。

Hello, and thanks for the very fast responses. I just realized I forgot to add some very relevant detail.

我的POST方法的参数就是我所说的信封对象,其中包含两个属性:listingId(INT)及说明(字符串)。当我添加了urlen codeD Content-Type头,并通过{listingId:1234,说明:一些介绍}'作为我的POST数据,在我POST方法env参数在服务器上为空(即,似乎无法解析我提供JSON)。对不起,这是从原岗位省略。

The parameter to my POST method is what I call an Envelope object, which contains two properties: listingId (int), and Description (string). When I add the urlencoded Content-Type header, and pass '{"listingId":1234, "Description":"some description"}' as my POST data, the env parameter in my POST method on the server is NULL (that is, it seems unable to parse the JSON I'm providing). Sorry this was omitted from original post.

推荐答案

我觉得这个职位将是有益的。

I think this post would be helpful.

<一个href=\"http://stackoverflow.com/questions/24710503/how-do-i-post-urlen$c$cd-form-data-with-http-in-angularjs/24964658#24964658\">How帖子发与的$ HTTP urlen codeD格式数据AngularJS?

默认情况下,$ http服务将改变由即将离任的要求
  串行化数据为JSON,然后与内容 - 张贴
  类型,应用/ JSON。当我们要张贴的值作为FORM
  后期,我们需要改变的序列化算法,并发布数据
  与内容类型应用程序/ x-WWW的形式urlen codeD。

By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json". When we want to post the value as a FORM post, we need to change the serialization algorithm and post the data with the content-type, "application/x-www-form-urlencoded".

这是从你修改的plnkr。您code缺少JSON转换为EN codeD网址。

This is the modified plnkr from yours. Your code is missing conversion of JSON to encoded url.

<一个href=\"http://plnkr.co/edit/4aeEDz73qgHSfOv1sBgn?p=$p$pview\">http://plnkr.co/edit/4aeEDz73qgHSfOv1sBgn?p=$p$pview

$http({
    method: 'POST',
    url: 'http://simpleApiEarl.azurewebsites.net/api/envelopes',
    data: env,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
      var str = [];
      for(var p in obj)
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
      return str.join("&");
    }
  }).

这篇关于非常简单AngularJS $ HTTP POST中的结果“400(坏请求)”和“无效的HTTP状态code 400'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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