0

I am using Jquery DataTable. And using making ajax calls (CROSS DOMAIN Request) like this to get the data:

ajax: {
    url: url,
    type: 'POST',
    dataType: 'json',
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
  }

This is how the header looks for this request in IE 8

Key Value
Request POST /api/data HTTP/1.1
Accept  */*
Origin  http://localhost:5000
Accept-Language en-US
Accept-Encoding gzip, deflate
User-Agent  Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)
Host    localhost:5555
Content-Length  3647
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache 

The Jquery DataTable automatically post the content to the server. But when the request is sent through IE 8, server is not receiving any data in HttpContext.Current.Request.Form object. I am using below code to read the data posted by the server.

            var formData = HttpContext.Current.Request.Form;
            var direction = formData["order[0][dir]"];
            var draw = Convert.ToInt32(formData["draw"]);
            var length = Convert.ToInt32(formData["length"]);
            var start = Convert.ToInt32(formData["start"]);

If I post request through chrome or FireFox, I get the data on the server. When the request is going through IE 8 content type header is not set. I think this is the reason why data is not available on the server side. Please help !!

SharpCoder
  • 18,279
  • 43
  • 153
  • 249

2 Answers2

0

You are setting dataType instead of contentType. As per the jQuery docs you need to use contentType when setting what the browser is sending and dataType when specifying what the browser is expecting to receive.

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

Type: String

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

RichieAHB
  • 2,030
  • 2
  • 20
  • 33
  • Thanks for the reply. I have tried setting contentType as well but looks like IE 8 does not send this information. Check this link point no 4. http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx – SharpCoder Sep 30 '14 at 10:16
  • Is it cross domain then? – RichieAHB Sep 30 '14 at 10:21
  • Have you seen this? http://stackoverflow.com/questions/3362474/jquery-ajax-fails-in-ie-on-cross-domain-calls – RichieAHB Sep 30 '14 at 10:33
  • Thank you for quick reply. I don't think its a CORS issues. As I am able to call the correct end point of the server. we are using jquery plugin to overcome CORD issue. Only problem is the content posted through request body, is not available under `HttpContext.Current.Request.Form`. Earlier we were calling the same endpoint as GET request and everything was working. Now we changed the request type to `POST`. I really appreciate your help. – SharpCoder Sep 30 '14 at 11:30
0

To answer my own question, On the server side, I am now reading the form data like this:

var formData = HttpUtility.ParseQueryString(await Request.Content.ReadAsStringAsync());

earlier I was trying to read data like this: var formData = HttpContext.Current.Request.Form;

As mentioned in this link point 4, IE 8 does not send contentType in the request header. And the link also talks about manually reading the request body which is what I am doing now.

SharpCoder
  • 18,279
  • 43
  • 153
  • 249