CRM 2011 – Ajax call using jQuery returns “No Transport” error
I was trying to call the OData service in CRM. I was using the good old jQuery $.getJSON function. It was failing with the “errorThrown” parameter telling me “No Transport”.
I had a vague recollection of hitting this error in a different context last year, so I looked it up and found that the error can be caused by a cross-domain Ajax request.
On the surface, this didn’t really explain my problem. My browser was pointing at my CRM installation and I was making a call to the OData service on the same installation. So why the cross domain issue?
I hunted a bit more and here is what I found: I was using the Xrm.Page.context.getServerUrl function to make sure I had the right server url BUT this function does not return the domain the browser is using. Instead it returns the domain that was set in the config of the CRM installation.
So if you navigated to the CRM installation using the domain name “crm” and the config says “crmserver03″, then calling getServerUrl will return a different domain to the one your browser is using. In this case, if you use getServerUrl to build the URL for an Ajax call then you will be using a different domain name and will get the “No Transport” error (unless you’ve done this: How to make a cross-domain Ajax call in jQuery).
I think that’s a bug in gerServerUrl.
Anyway. Here is my fix:
var url = window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
Or, for a relative url, you can use:
var url = "/" + Xrm.Page.context.getOrgUniqueName();
Whichever suits your needs.
Update: See comment below from Carsten Groth and my reply for a new Xrm function that returns the correct url.