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.

Sitecore: Adding your own buttons to the RichText editor

It’s surprisingly easy to add your own functionality to the Sitecore Rich Text editor. First you need to register your button in Sitecore:

/system/settings/Html Editor Profiles/Rich Text Default/Toolbar 1

Duplicate an existing button and you will have added a new button to the first toolbar in the default Rich Text Editor.
Change the “Click” field of your new button to a unique text. I have changed mine to “MyNewButton”.

Then you must change the Rich Text javascript file. You’ll find the file at:

/sitecore/shell/Controls/Rich Text Editor/RichText Commands.js

This file contains the commands for the Rich Text editor. Here you can add your own “RadEditorCommandList” for the “MyNewButton”: 

RadEditorCommandList["MyNewButton"] = function(commandName, editor, tool)
{
};

Inside this function you can add code to add text to the editor:

var str = "This is my new text. This will be inserted at the cursor position";
editor.PasteHtml(str);

Here is another example where I call a webservice and inserts the response:

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","/service.asmx/MyFunction",false);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("param=" + escape("my parameter"));
if (xmlhttp.status==200)
{
  var str = xmlhttp.responseText;
  str = str.replace(/</g, "");
  editor.PasteHtml(str);
}
else
{
  alert("Error calling webservice: " + xmlhttp.statusCode + " - " + xmlhttp.statusText);
}

Follow

Get every new post delivered to your Inbox.

Join 92 other followers