Hello all,
I'm in the process below of trying to learn how to use Odata by making a simple request for data from another entity within my CRM 2011 online environment. I have two problems though they may be related.
1. When I run my below code, as soon as the XMLHttpRequest retrieveReq uses its send() function, internet explorer (and also Firefox and Chrome) prompt me for a username and password. Given I'm already logged in I don't understand why this would happen and I can't seem to find any articles that explain how to fix this.
2. My second problem is that if I simply click cancel or enter some user information to get past the credentials prompt, I then get the javascript error " Invalid character, character 9" on the line "var retrieved = JSON.parse(retrieveReq.responseText).d;" in the below solution which again I don't understand why.
Also the function tryRest() below is triggered when I select a value from a lookup field "new_certificate".
I'm quite new to this stuff so all your help is appreciated. Thanks in advance!
function tryRest() { var serverUrl = document.location.protocol + "//" + document.location.host + "/XRMServices/2011/OrganizationData.svc"; var newCertificateid = Xrm.Page.getAttribute("new_certificate").getValue(); var newCertificateid = primarycontact[0].id; var newCertificateid = newCertificateid.replace('{', '').replace('}', '').toLowerCase(); // Creating the Odata Endpoint var oDataPath = serverUrl var retrieveReq = new XMLHttpRequest(); var Odata = oDataPath + "/new_certificateSet$select=new_CertificateClassSUFFOLk&$filter=new_certificateId eq guid'"+newCertificateid+"'"; retrieveReq.open("GET", Odata, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); }; retrieveReq.send(); } function retrieveReqCallBack(retrieveReq) { if (retrieveReq.readyState == 4 /* complete */) { var retrieved = JSON.parse(retrieveReq.responseText).d; var output = retrieved.results; // Alert the output from new_certificate object. Note I've yet to configure this - the program won't get here currently. alert(output); } }