jQuery AJAX test

Thanks, Have a great day ahead,Let the Force be with you! Please mark this as best answer if it helps you.  ", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4FYqSAN", "datePublished": "2016-01-28T05:55:04.000Z", "author": { "@type": "Person", "name": "Vinod ZSL KR", "url": "https://trailblazers.salesforce.com/profileView?u=00530000004esbwAAA", "affiliation": { "@type": "Organization", "name": "ZSL" } } }, "suggestedAnswer": [ { "@type": "Answer", "text": "I am getting this error in console POST https://sfdc-cors.herokuapp.com/services/oauth2/token 400 (Bad Request) Can any one help  ", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4FYqSAN", "datePublished": "2016-03-07T10:38:40.000Z", "author": { "@type": "Person", "name": "Developer Forums User (Inactive)", "url": "https://trailblazers.salesforce.com/profileView?u=0054V00000H86IwQAJ", "affiliation": { "@type": "Organization", "name": "Salesforce Developer Forums" } } } ] } }
Skip to main content
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:- 

$.ajax({

                    type: 'GET',

                    url: 'https://login.salesforce.com/services/oauth2/token',

                    contentType: 'application/json',

                    dataType: 'json',

                    beforeSend: function(xhr) {

                        xhr.setRequestHeader('grant_type','password'),

                        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.

                

 
23 answers
  1. Jan 28, 2016, 5:55 AM
    Hi Hemant,

    Try this it worked for me.

    <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

    <script>

    $(document).ready(function(){

        $("button").click(function(){

            loginsf(function(data) { 

               $.ajax({

                  type: "GET",

                  contentType: 'application/json',

                  url:'https://sfdc-cors.herokuapp.com/services/apexrest/prefix_if_exsists_for your_org_else_dont_add/RestAPI/',  

                  cache :false,

                    headers: {'Authorization': 'Bearer '+data},

                  dataType: 'json',

                  success: function (data) {

                      

                  },

                  error : function(jqXHR, textStatus, errorThrown) {

                      console.log('Error: '+jqXHR.status);

                      console.log('textStatus: '+textStatus)

                     } 

               });

            });    

        });

    });

    function loginsf(fn){

         $.ajax({

                type: 'POST',

                crossOrigin: true,

                url: 'https://sfdc-cors.herokuapp.com/services/oauth2/token',

                dataType: 'json',

                cache :false,

                data : {"grant_type":"password","client_id":"add_your_client_id", "client_secret":"your_client_secret","username":"Your_username","password":"your_password_and secritytoken"},

                success : function (data) {

                 fn(data.access_token);

                },

                error : function (data, errorThrown,status) {

                }

        });

      }

        

    </script>

    </head>

    <body>

    <div id="div1"><h2>jQuery AJAX test</h2></div>

    <button>Get External Content</button>

    </body>

    </html>

    Thanks,

    Have a great day ahead,Let the Force be with you!

    Please mark this as best answer if it helps you.

     
0/9000