My Software Notes

Useful things I discover

CRM 2011 – Ajax call using jQuery returns “No Transport” error

with 2 comments

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.

For Example:

Your browser url is “http://crm/MyCorp” and your Ajax call uses http://server03/.

Gotcha

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?

Then it hit me.  There were two ways to get to the CRM installation.  There was a DNS entry for “crm” and there was one for the server itself called “crmserver03″.  I checked what url was being used by running the JavaScript debugger (good old F12) and there was the answer:  My browser was pointing at http://crm/MyCorp and the Ajax call was going to http://crmserver03/MyCorp.  Huh?

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.

See:  How does “Xrm.Page.context.getServerUrl” work?

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.

About these ads

Written by gsdwriter

January 7, 2013 at 1:48 pm

2 Responses

Subscribe to comments with RSS.

  1. Use getClientURL should fix this

    Carsten Groth

    February 7, 2013 at 3:45 pm

    • Thanks Carsten.

      getClientUrl is a new function that was only recently added. As the docs say: “This method is new in Microsoft Dynamics CRM 2011 Update Rollup 12 and the Microsoft Dynamics CRM December 2012 Service Update.”

      Nice to see that Microsoft is listening to us whining developers :)

      gsdwriter

      February 8, 2013 at 1:22 pm


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: