title: When performing post via ajax, Bad Request is returned instead of the JSON result
body: <h1>Javascript</h1>
  
  <pre><code>jqXHR = $.ajax({ url: $frm.attr("action"), type: "POST", dataType: "json", cache: false,
    headers: headers, contentType: "application/json;charset=UTF-8", data: ko.mapping.toJSON(data, map),
    beforeSend: function(x) {
      if (x &amp;&amp; x.overrideMimeType) {
        return x.overrideMimeType("application/json;charset=UTF-8");
      }
    }
  });
  
  jqXHR.fail(function(xhr, err, msg) {  /* xhr.responseText  NEED TO BE JSON!!! */ });
  </code></pre>
  
  <h1>In Chrome</h1>
  
  <p><strong>Headers</strong></p>
  
  <pre><code>Request Method:POST
  Status Code:400 Bad Request
  Request Headersview source
  Accept:application/json, text/javascript, */*; q=0.01
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4
  Connection:keep-alive
  Content-Length:10
  Content-Type:application/json;charset=UTF-8
  User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
  X-Requested-With:XMLHttpRequest
  Request Payloadview source {Id:0}
  Response Headersview source
  Cache-Control:private
  Content-Length:54
  Content-Type:application/json; charset=utf-8
  Date:Thu, 27 Feb 2014 14:01:59 GMT
  Server:Microsoft-IIS/8.0
  X-AspNet-Version:4.0.30319
  X-AspNetMvc-Version:5.1
  X-Powered-By:ASP.NET
  </code></pre>
  
  <p><strong>Response</strong></p>
  
  <blockquote>
    <p>[{"Name":"Nome","ErrorMessage":"campo obrigatório."}]</p>
  </blockquote>
  
  <p><strong>Works in chrome!</strong></p>
  
  <hr>
  
  <h1>In IE8</h1>
  
  <p><strong>Headers (Request)</strong></p>
  
  <pre><code>POST /Motivos/Salvar HTTP/1.1
  Accept: application/json, text/javascript, */*; q=0.01
  Accept-Language: pt-br
  x-requested-with: XMLHttpRequest
  Content-Type: application/json;charset=UTF-8
  Accept-Encoding: gzip, deflate
  User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
  Content-Length: 10
  Connection: Keep-Alive
  Pragma: no-cache
  </code></pre>
  
  <p><strong>Headers (Response)</strong></p>
  
  <pre><code>HTTP/1.1 400 Bad Request
  Cache-Control: private
  Content-Type: text/html
  Server: Microsoft-IIS/8.0
  X-AspNetMvc-Version: 5.1
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Date: Thu, 27 Feb 2014 13:51:46 GMT
  Content-Length: 11
  
  Bad Request
  </code></pre>
  
  <p><strong>NOT WORK!!</strong></p>
  
  <h1>Asp.net MVC</h1>
  
  <h2>Filter</h2>
  
  <pre><code>public class HandleExceptionAttribute : HandleErrorAttribute
  {
      public override void OnException(ExceptionContext filterContext)
      {
          if (filterContext.HttpContext.Request.IsAjaxRequest() &amp;&amp; filterContext.Exception != null)
          {
              filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
              var ex = filterContext.Exception.GetBaseException();
              filterContext.Result = new JsonResult
              {
                  JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                  Data = new
                  {
                      ex.Message,
                      ex.GetType().Name
                  }
              };
              filterContext.ExceptionHandled = true;
          }
          else
          {
              base.OnException(filterContext);
          }
      }
  }
  </code></pre>
  
  <p><strong>Apply on GlobalFilterCollection</strong></p>
  
  <h2>Controller</h2>
  
  <pre><code>[ValidateJsonAntiForgeryToken, HttpPost]
  public virtual JsonResult Salvar(TViewModel viewModel)
  {
      if (ModelState.IsValid)
      {
          TEntity model;
          if (default(TKey).Equals(viewModel.Id))
          {
              model = Mapper.Map&lt;TEntity&gt;(viewModel);
              AdicionarEntidade(model, viewModel);
          }
          else
          {
              model = Repositorio.Get(viewModel.Id);
              Mapper.Map(viewModel, model, typeof(TViewModel), typeof(TEntity));
              SalvarEntidade(model, viewModel);
          }
  
          return SalvarResult(model);
      }
  
      Response.StatusCode = 400;
      return Json(ModelState.ToJson(), JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <h2>Extenssion</h2>
  
  <pre><code>public static object ToJson(this ModelStateDictionary dic, params string[] othersMessages)
  {
      var states = (from e in dic where e.Value.Errors.Count &gt; 0
                    select new { Name = e.Key, e.Value.Errors[0].ErrorMessage }).ToList();
  
      if (othersMessages != null)
          foreach (var message in othersMessages)
              states.Add(new { Name = "", ErrorMessage = message });
  
      return states;
  }
  </code></pre>
  
  <h1>Questions</h1>
  
  <ul>
  <li>Why not have the xhr.resposeText object?   </li>
  <li>How to retrieve JSON in the same way that I recover in Chrome?   </li>
  </ul>
  
  <p>I need the JSON to populate the form!</p>
  
  <h1>Notes: 03/11/2014</h1>
  
  <p>When I add <code>Response.TrySkipIisCustomErrors = true;</code> in my controler, it works! 
  responseText returns the json. 
  Why?</p>