• Awais Ilyas
  • 0 Points
  • Member since 2016

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
I am calling a rest API from javascript from third party appplication outside salesforce and  getting this issue:-

XMLHttpRequest cannot load https://login.salesforce.com/services/oauth2/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 400.

My Code:- 
                    type: 'GET',
                    url: 'https://login.salesforce.com/services/oauth2/token',
                    contentType: 'application/json',
                    dataType: 'json',
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader('client_id',  'CLIENT_ID'),
                        xhr.setRequestHeader('client_secret', 'LIENT_SECRET'),
                        xhr.setRequestHeader('username', 'Username'),
                        xhr.setRequestHeader('password', "Password+securityToken")
                    success: function(response) {
                        console.log('Successfully retrieved ' + response);
                        //Other logic here
                    error: function(response) {
                        console.log('Failed ' + response.status + ' ' + response.statusText);
                        //Other logic here

I have made an Connected app adn provide access to that user in Profile settings.

Here are some of my questions:-
1. The callback url in connected app can be http://localhost or should be a proper server URL.
2. Do i need to add http://localhost(server URL) in CORS and REMOTE settings
3. Server URL should be HTTPS (secured)

Please help me in this.

Thanks in advance!
4. I mentioned my error above , please help me in resolving it.