来自Body的Web API对象在IE中是空的

时间:2013-11-27 04:02:20

标签: asp.net internet-explorer asp.net-web-api

我有一个asp.net Web表单应用程序,我正在尝试为web api控制器调用ValidateRegistration1方法。它在Firefox和Chrome中表现不错,但是当我使用IE时,该对象为空。内容类型和其他似乎几乎相同。我找不到问题。

fiddler中的Firefox POST:

{Method: POST, RequestUri: 'http://localhost:5555/api/accounts/registervalidate1', Version: 1.1, Content: System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent, Headers:
{
  Cache-Control: no-cache
  Connection: keep-alive
  Pragma: no-cache
  Accept: */*
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Accept-Language: en-US
  Accept-Language: en; q=0.5
  Cookie: __AntiXsrfToken=0871e412cceb47faa968c5563d19f8e1; ASP.NET_SessionId=glwlyn2mdihne0ev4uhc5fnv
  Host: localhost:5555
  Referer: http://localhost:5555/
  User-Agent: Mozilla/5.0
  User-Agent: (Windows NT 6.3; WOW64; rv:25.0)
  User-Agent: Gecko/20100101
  User-Agent: Firefox/25.0
  X-Requested-With: XMLHttpRequest
  Content-Length: 207
  Content-Type: application/x-www-form-urlencoded; charset=UTF-8
}}

提琴手中的IE POST:

{Method: POST, RequestUri: 'http://localhost:5555/api/accounts/registervalidate1', Version: 1.1, Content: System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent, Headers:
{
  Connection: Keep-Alive
  Pragma: no-cache
  Accept: */*
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Accept-Language: en-US
  Accept-Language: en; q=0.7
  Accept-Language: tr; q=0.3
  Cookie: __AntiXsrfToken=4772bed0c8e243f4a7893794ed176f47
  Host: localhost:5555
  Referer: http://localhost:5555/
  User-Agent: Mozilla/5.0
  User-Agent: (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)
  User-Agent: like
  User-Agent: Gecko
  X-Requested-With: XMLHttpRequest
  Content-Length: 0
  Content-Type: application/x-www-form-urlencoded; charset=UTF-8
}}

web api方法

    [Route("registervalidate1")]
    //[ResponseType(typeof(Account))]
    [HttpPost]
    public HttpResponseMessage ValidateRegistrationStep1(HttpRequestMessage request, 
        [FromBody]AccountRegisterModel a)
    {

        HttpResponseMessage response = null;
        List<string> errors = new List<string>();

        bool isEmailFree = _accountService.EmailInUse(a.LoginEmail);
        if (isEmailFree)
            errors.Add("jj.");

        if (errors.Count == 0)
            response = request.CreateResponse(HttpStatusCode.OK);
        else
            response = request.CreateResponse<string[]>(HttpStatusCode.BadRequest, errors.ToArray());
        return response;

    }

1 个答案:

答案 0 :(得分:2)

我刚遇到同样的问题。对我来说,修复是手动将“application / json”(contentType: 'application/json')的内容类型添加到我的jQuery ajax调用中。我相信你现在已经找到了解决方案,但是我发布了同样问题的其他人。