IE7, AJAX, And 400 “Bad Request”
I spent bout two hours today tring to figure out why I kept getting a weird error while using prototype to call a WCF service.
After that, any refreshing of the page would return 400/”Bad Request” errors, mysteriously. Just to make sure that it wasn’t isolated to prototype, I also tried jQuery as well. Still nothing.
Well, as it turns out, that setting the AJAX request content type to application/json wasn’t enough; I had to set it to application/json; charset=utf-8.
So if you’re encountering weird issues with any AJAX library, IE7, and WCF and/or ASP.NET, be sure to check your content type setting.
Update 6/20/2008: bah! I woke up this morning and it’s not working again! Oddly, it works fine if I have Fiddler running and as soon as I shut off Fiddler, it stops working. To complicate matters even more, it’s only happening on methods where I have post content.
Update 6/22/2008: after further investigation, I’m concluding that it’s related to SharePoint. To isolate the different components, I created a custom WCF service and a simple runtime which models our current custom WCF runtime. When I used a plain HTML page or even an ASPX page with a script manager on it, prototype AJAX requests worked fine. Okay, so it wasn’t how we were hosting the service and it wasn’t an issue with a collision between ASP.NET AJAX and prototype. I then hooked a SharePoint layout page up to the same service and boom, it all breaks :-S
I’ve broken it out into the following scenarios:
- Using FireFox, the requests always work. No matter how many times I refresh the page, the AJAX request always works fine.
- Using IE7, it will always work on the first load of the IE process.
- If I hit CTRL+F5, it will continue to work.
- If at any point, I hit F5, it will fail and even CTRL+F5 will not fix it.
- If I have Fiddler running, it will always work, even if it entered a failure state after hitting F5! (So Fiddler must be doing something to the HTTP message??). As far as I can tell, the only difference between the Fiddler request and the native request, from viewing the WireShark trace, is the “Authorization” header and the fact that the failed request has a “Content-length” header value of 0 (manually setting the header doesn’t work either). I verified that prototype was not somehow mangling the POST body content by writing a trace of the XHR request body right before and right after the request is sent (both came out okay).
One of the really weird things about this error is that it cannot be observed when I have Fiddler running; somehow, Fiddler “fixes” the issue. I had to hook up WireShark on the server and watch the raw TCP messages to finally see that on the unsuccessful attempts, the POST content to my WCF service was empty. The method required one parameter, so the WCF serializer threw a formatter excpetion when it received an empty message:
I’ve tried playing around with various cache avoidance strategies to no avail (setting the “Cache-Control” header, setting the “If-Modified-Since” header, setting the “Pragma” header, addin randomness to the POST URL) so for now, I’m just going to have to give up on this. I’ve confirme the error using three different OS environments (XP SP2 (desktop), XP64 SP2 (dev), and 2003 R2 SP2 (VM)), all running IE 7.0.5730.11.
I know there are various articles out there regarding IE issues with XHR and how the ordering of the open(), onreadystatechange, and send() operations must be called; however, I verified in the prototype source (lines 1213-1223) that the correct order of operations are being performed.
Thinking that perhaps some Microsoft mojo was behind all of this, I gave the Sys.Net.WebRequest a shot. This also did not yield any postive results. Oddly, ASP.NET AJAX to web service calls all work fine (we had some existing AJAX components which were calling to a web service, but the goal was to transition away from writing these “empty” proxy services and call directly to the WCF service).
If any SharePoint development team members or program managers are reading this, please look into it! I’m not sure if it affects IIS hosted WCF services at this point, but a sample project can be downloaded from here: WcfAjaxWonkiness.zip. Create a layout page which calls the service and add it to SharePoint and observe it blowing up.