问题
I can successfully reach XRMServices on ORGANIZATION_URL/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber
and retrieve customer account number on a browser after logging in. However, there is an authentication service blocking this if I use AJAX. My code is as below
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ORGANIZATION_URL+ "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber,Telephone1,Telephone2,new_CustomerDiscGroup,EMailAddress1,EMailAddress2,EMailAddress3",
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
console.log(XMLHttpRequest);
},
complete: function (XmlHttpRequest) {
console.log(XMLHttpRequest);
},
success: function (data, textStatus, XmlHttpRequest) {
console.log(data);
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
console.log(textStatus);
}
});
What am I missing???
回答1:
Most likely you are doing cross-site scripting error. So you are opening your CRM using for example http://localhost or http://ip_number, and then in your ajax call you are using ORGANIZATION_URL which is probably different (for example http://contosocrm). Make sure that you are calling your ajax request with exactly the same address as you use to access CRM (or the page that is calling ajax)
回答2:
After a huge pain, here is the answer to logging in to Dynamics CRM using jQuery
$.ajax({
url : 'https://<Your Authentication URL for CRM>/adfs/ls',
data : {
UserName : '<username>',
Password : '<password>',
wa : 'wsignin1.0',
wauth : 'urn:federation:authentication:windows',
wtrealm : '<CRM Location>',
wct : 'YYYY-MM-DDTHH:MM:SSZ'
},
headers : {
Accept: 'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap,application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*','Content-Type': 'application/x-www-form-urlencoded','Access-Control-Allow-Origin' : '*'
},
crossDomain: true,
dataType: 'jsonp',
beforeSend : function(xhr){
console.log(xhr);
},
complete : function(xhr){
console.log(xhr);
},
success : function(xhr){
console.log(xhr);
},
error : function(xhr){
console.log(xhr);
}
});
Hope that helps someone
来源:https://stackoverflow.com/questions/43212475/retrieving-records-in-dynamics-crm-after-logging-in-using-ajax-jquery