2

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!!

1

1 Answer 1

0

Are you sure there is no way around calling that particular REST API?

Using the good ol ajax toolkit can be pretty versatile since you are on the Salesforce server. http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_more_samples.htm

Not the answer you're looking for? Browse other questions tagged .