Ajax and FBML

Jul 8, 2008 at 9:32 PM
We're having trouble getting Facebook Ajax to play nicely with ASP.NET Web Services in an FBML/FBJS page.  Due to Facebook’s Javascript security model, we are required to use Facebook’s own javascript Ajax object for making asynchronous calls – we cannot use typical ASP.NET means such as ScriptManager or by importing MicrosoftAjax.js. The Facebook Ajax object (docs here) only makes HTTP POSTs using application/x-www-form-urlencoded content headers. However, as far as we know, ASP.NET [WebMethod] Web Services will automatically reject any POSTs that are not of content-type application/json – described as a security feature on ScottGu’s blog here. Is there any way to get a Web Service (asmx) to respond to an application/x-www-form-urlencoded HTTP POST? Or to bend Facebook Ajax to our will?

- Why can’t we use ScriptManager or MicrosoftAjax.js?  Part of Facebook’s Javascript security model involves sandboxing all javascript with an application hash, so that myfunction() becomes aAPP_ID_NUMBER_myfunction(). This wreaks havoc on ASP.NET AJAX, which depends on being able to inject MicrosoftAjax.js into the page and extend base Javascript objects like Date and Function. These get hashed to aAPP_ID_NUMBER_Date etc., and throw errors.
- A potential backup solution is to migrate the asmx into an aspx page and POST to the aspx instead, as described here. However, this approach goes against ASP.NET design principles and would require us to develop our own serialization protocol. We’re hoping there’s a simpler answer.
Jul 8, 2008 at 11:25 PM
Hi msbernst

I haven't worked with fbjs, but I have just gotten a similar scenario to work, namely feedstory forms. I'm looking at a wireshark snort right now and it seems facebook also posts "application/x-www-form-urlencoded" for these requests. Getting my WCF webservice to return json in response to these requests involved the ritual sacrifice of most of my extended family so I'm not gonna give up this nugget of gold in a public forum... (ha! gotcha :-)).

I have something like the following:

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class feedHandler
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        public FeedResponse GetResponse()

In the behaviour section of your web.config, you also have to use "<webHttp/>" instead of "<enableWebScript />". If you get this to work, could you please reply to this post -- I would love to know whether this is viable for fbjs or not...

Aug 20, 2008 at 11:10 PM

Hi friism,

Msbernst and Idid indeed get it working with a WCF web service (as opposed to an ASP.NET web service).  We now have a IIS-hosted WCF WS that happily receives and returns calls from the Facebook Ajax object.  The key parts are:


public interface IMyService
WebInvoke(Method = "POST", UriTemplate = "MyApi?myParam={strParam}", ResponseFormat = WebMessageFormat.Json)
Int32 MyApi(string strParam);

with a webHttp behavior in web.config.  Thanks!


Jun 12, 2009 at 11:48 AM

Have any of you been able to submit a JSON request to a multi-parameter function? Getting it to work with 0-1 parameter is easy enough, but I can't seem to make it work with multiple parameters, i.e.


string MyFunction(string param1, string param2);