简体   繁体   中英

Dynamics CRM 2011 REST API using jQuery

I personally don't have any experience with CRM 2011 and only recently found out about the REST oData API so decided to have a crack at it using basic jQuery ajax calls. I created a basic html page and put the following code in there from snippets that I picked up from various Google searches. Seems like a fairly straightforward call:

var serverurl = 'http://[OrganizationUrl]/XRMServices/2011/OrganizationData.svc/[PrivateEntity]?$select=[PrivateEntity_Field1],[PrivateEntity_Field2]';

$.ajax({
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Accept', 'application/json');
    },
    url: serverurl,
    type: 'GET',
    dataType: 'jsonp',
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert('success');
    },
    error: function (xhr, status, error) {
        alert("Error : " + status);
    },
}); 

All this does is go to the error callback and alert "Error: parseerror".

A closer inspection on FireBug yields the following request and response headers: 萤火虫输出

Despite explicitly specifying json in the header and content it still brings back xml/atom feed and still goes the the error callback instead of the success.

Does this JavaScript have to be executed from within CRM ie deployed to CRM first? Or can it work as I've done from an external self contained HTML page? I'm familiar with jQuery but not familiar with CRM or this REST API so a little out of my depth here. Any help is appreciated

UPDATE : I've executed the following code in fiddler and it brings back what I expect from CRM:

GET http://[OrganizationUrl]/XRMServices/2011/OrganizationData.svc/[PrivateEntity]?$select=[PrivateEntity_Field1],[PrivateEntity_Field2] HTTP/1.1
User-Agent: Fiddler
Host: melmd0105:5555
Accept:  application/json

CRM 2011 REST endpoint is only available for Web Resources, this means that have to be executed from within CRM. Please refer to this page:

http://msdn.microsoft.com/en-us/library/gg334279.aspx

under Limitations you will find this advice:

Use of the REST endpoint is limited to JScript libraries or Silverlight web resources.

I'll agree with Guido's answer that Microsoft says it is limited to Jscript or Silverlight, but I will say that LinqPad has figured out a way to authenticate, and you can actually write linq queries to generate your RestURL. This has been the fastest method for me to generate Rest URLs for CRM 2011 (only works with On Prem).

NOTE I have attempted to use Fiddler to determine what they are doing to authenticate, but with no success.

Here is an SO question using LinqPad and CRM:

How to perform an ODATA expand in LinqPad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM