14

Why does not my OData jQuery Dynamic CRM On-Line query work?

 2 years ago
source link: https://www.codesd.com/item/why-does-not-my-odata-jquery-dynamic-crm-on-line-query-work.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Why does not my OData jQuery Dynamic CRM On-Line query work?

advertisements

I have a web page that needs to obtain information from Dynamics CRM2011 on-line using jQuery. As a test, I've just tried to retrieve all of the accounts from a test system. I've used some code that I've tested within the CRM product, and it works.

When I try to run the same jQuery code in a separate web page that is not on the CRM domain it doesn't work (no transport) so I added the $.support.cors = true line to allow cross-site scripting. Now when it runs it pops up a dialog box asking for credentials. However, when I enter my credentials for CRM on-line, it doesn't accept them. After three attempts I get the error, "unauthorized".

Can anyone help?

Here's the code:

$(document).ready(function()
{

var request = "https://myorganisation.crm4.dynamics.com/XRMServices/2011/OrganizationData.svc/AccountSet";
$.support.cors = true;

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: request,
    async: false,
    beforeSend:
        function (XMLHttpRequest)
        {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
    success:
        function (data, textStatus, XmlHttpRequest)
        {
            var result = data.d.results[0];
            alert('Success');
            alert(result);
            alert(result.Name);
        },
    error:
        function (XmlHttpRequest, textStatus, errorThrown)
        {
            alert(textStatus);
            alert(errorThrown);
        }
    });
});


The oData endpoint doesn't support cross site requests. It is designed to only support authentication from within the CRM application using either Silverlight or JavaScript Web Resources.

Take a look at this MSDN article for more info.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK