Resolved : Permission Denied error while accessing OrganizationService.svc from JavaScript in CRM 2011

Hi,

I was getting the Permission Denied error in one of our JavaScript that was making Ajax call to OrganizationService.svc. Obviously it was because of some cross domain thing. The problem was that it was working fine in one of our organization but gave error when deployed it in a new organization.

Intially using the below code to set the organization service url

var pagecontext = Xrm.Page.context;

var serverUrl = pagecontext.getServerUrl();

var serviceUrl = serverUrl + “/XRMServices/2011/Organization.svc/web”;

Changing it to following resolved the issue for us

var serverUrl = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName();

var serviceUrl = serverUrl + “/XRMServices/2011/Organization.svc/web”;

Hope it helps