Oracle CRM On Demand REST API Developer's Guide > Getting Started with the Oracle CRM On Demand REST API >

Using JavaScript to Send REST API Requests


You can use an XMLHttpRequest JavaScript object to send REST API requests to the Oracle CRM On Demand Server. An XMLHttpRequest object allows you to send GET, POST, PUT or DELETE requests. Your request can also include the format for the response, for example, JSON or XML.

The example in this topic is a sample of JavaScript code that makes a REST API call to get the Accounts collection. The JavaScript then converts the response output into a JSON object and outputs a screen alert with the number of accounts in the collection that is visible to the user.

In the example below, accounts are accessed using the default Accounts tag name, that is, Accounts has not been renamed or customized. In addition, the visible accounts do not exceed the default limit of 100 for the user, because the example does not use the limit or paging arguments. For an example of retrieving accounts using limit and paging arguments, see Retrieving a Collection Top-Level Resource List.

To use JavaScript to send REST API requests to the Oracle CRM On Demand Server, you must have access to Oracle CRM On Demand and be successfully logged into the Oracle CRM On Demand Server. For more information about REST API privileges, see Oracle CRM On Demand REST API Privilege Required. For more information about logging into Oracle CRM On Demand, see Login with Oracle CRM On Demand REST API.

var xhr = new XMLHttpRequest();
xhr.open("GET", "/OnDemand/user/Rest/latest/Accounts", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var respJSON = JSON.parse(xhr.responseText);
alert(respJSON.Accounts.length);
} else {
//console.error(xhr.statusText);
//handle error status
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);

Oracle CRM On Demand REST API Developer's Guide, Release 32 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Legal Notices.