I need to call an authenticated Apex Rest API from Site.com page. The way I am trying is using Username-Password Oauth authorization flow, to get access token.
Below is the javascript code I am trying,
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'https://login.salesforce.com/services/oauth2/token',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded'
},
data: {
'grant_type': 'password',
'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxx',
'client_secret': 'xxxxxxx',
'username': '[email protected]',
'password': '*******'
},
success: function(response) {
console.log('Successfully retrieved ' + response);
//Other logic here
},
error: function(response) {
console.log('Failed ' + response.status + ' ' + response.statusText);
//Other logic here
}
});
});
But when I preview the site page, I receive a Cross Domain Access error in the browser console,
XMLHttpRequest cannot load https://login.salesforce.com/services/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://nikhil-demo-dev-ed--sitepreview.ap2.force.com' is therefore not allowed access.
Does anybody have a solution for this issue?
Thanks in advance!!