Could not post to my webservice

Have you noticed that when calling a web service from JavaScript using POST (or GET for that matter), the call will work fine on your local development server, but when the thing is put into production it will fail?

This is because HTTP GET and HTTP POST are disable by default in .NET (from version 1.1).
This article (http://support.microsoft.com:80/default.aspx?scid=kb;en-us;819267) explains the situation.

The symptoms are as follows:

  • The statuscode of the XMLHTTP call is “undefined”.
  • The statustext of the XMLHTTP call is “Internal Server Error”.
  • You get the following exception: Request format is unrecognized for URL unexpectedly ending in ‘/MyFunction‘.

In order to fix this issue you will have to enable HTTP POST (and maybe also HTTP GET) by adding a declaration to the web.config. I prefer to add a local web.config along with my web service. Simply put a web.config in the same folder as your .asmx file with the following contents:

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    <compilation debug="false"></compilation>
  </system.web>
  <system.codedom>
  </system.codedom>
  <!-- 
    The system.webServer section is required for running ASP.NET AJAX under Internet
    Information Services 7.0.  It is not necessary for previous version of IIS.
  -->
  <system.webServer>
  </system.webServer>
</configuration>

This configuration file allows HTTP POST calls to be executed on all services installed in the same folder. To allo HTTP GET calls, simply add HttpGet to the file as well:

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

You could also add the XML to the web site’s web.config, but this will allow all HTTP GET and HTTP POST calls to all services, and you might not want this.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s