Search code examples
jqueryhttpwcffirefox

HTTP 400 on PUT Request for Firefox/IE. Works correctly for Chrome/Opera


There's a website using jquery to call a WCF Webservice on another domain (Utlising CORS). Most of the methods are GET ones but there are a couple of PUT ones.

All GET methods work correctly on all browsers. PUT methods however will only work for Chrome and Opera. For Firefox and Internet Explorer they will result in a HTTP 400 BAD REQUEST error. I have read everything and tried everything to change the CORS settings of the web services with no success.

How differently can browsers be making these PUT calls if they work flawlessly for some and not for others?

For reference, the webservice Global.asax file looks like that:

 protected void Application_BeginRequest(object sender, EventArgs e)
    {

        //CORS ENABLED
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");

            HttpContext.Current.Response.End();

        }
    }

and the web.config relevant session is like that:

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
   <httpProtocol>

   </httpProtocol>
  </system.webServer>

Any ideas?

Edit: This is there jQuery Request:

       addAbs = function addAbs() {
        return $.ajax({
            type: 'PUT',
            url: Constructor.prototype.getServletURL(),
            contentType: "application/json; charset=utf-8",
            headers: {"Origin": "http://app-eservices.talentia-software.com"},
            data: JSON.stringify(newAbsence),
            timeout: GlobalContext.xhrTimeout,
            beforeSend: self.showLoadingDialog('')
        });

Solution

  • Turns out the reason was the AJAX Request specified a contentType of "charset=utf-8" , instead of "charset=UTF-8", so capitalisation made the difference. For reference, the capitalised version is the correct one, but both are supposed to work.

    This was not an issue for Chrome which auto-capitalised it, but for Firefox and other browsers that did not change the capitalisation, the request would end up with a HTTP 400 Bad Request error. Apparently WCF services can only accept the capitalised version which is weird. Other than changing the AJAX request, does anyone know if there's another way to make the WCF services accept the lowercase version?