Announcements
Hi All,
I am using Microsoft Dynamics 2016 Online.
I am have a HTML in which I have a javascript which calls a CRM WEB API and filters w.r.t fullname. Here is my code
var searchBoxSelectedValue = window.parent.Xrm.Page.getAttribute("new_name").getValue();
var req = new XMLHttpRequest();
req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/contacts?$select=fullname&$filter=fullname eq" + searchBoxSelectedValue, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var mydata = results.value[i]["fullname"];
}
} else {
alert(this.statusText);
}
}
};
req.send(); // error shown in this line
I am getting an error saying "Bad Request" 400.
req.onreadystatechange = function() {} does not get executed, it gets skipped.
thanks in advance.
Regards,
Rahul
*This post is locked for comments
Hi All,
I got this problem solved i was erring with single quotes. here is my corrected line:
req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/contacts?$select=fullname&$filter=fullname eq " + "'"+ searchBoxSelectedValue + "'", false);
Regards,
Rahul
Have you tried to debug your code and check if query is forming well ?? you can debug js by writing debugger only, check your query while debugging what's is comming or share it here.
Thanks
I tried in CRM rest builder, in there inside code editor i created a variable and tried. Still giving me bad request.
Hi,
you can double check your query with the CRMRestBuilder: github.com/.../CRMRESTBuilder
I think your URL is not correct. Please check below things:
1. window.parent.Xrm.Page.context.getClientUrl() if this is returning the CRM URL.
2. v9.0 is the correct api version for your environment.
3. Please give a space between eq and ".
André Arnaud de Cal...
293,465
Super User 2025 Season 1
Martin Dráb
232,586
Most Valuable Professional
nmaenpaa
101,158
Moderator