Cross origin issue in salesforce response(Access-Control-Allow-Origin)

4.3k views Asked by At

I tried to get records from salesforce from external local files using JS. I can see a response in network tab. i received an error message in console:

"XMLHttpRequest cannot load https://login.salesforce.com/services/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."

MYCODE:

$.post("https://login.salesforce.com/services/oauth2/token",
{
    grant_type:"password",
    dataType : 'jsonp',
    headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"},
    client_id:"CLIENTID",
    client_secret:"CLIENTSECRET",
    username: "uname",
    password: "password"
},
function(data,status){
    //my_function(data);
    console.log(data);
});
function my_function(data){
    alert(data);
}

Any help and suggestions.

1

There are 1 answers

3
ricky On BEST ANSWER

Add crossOrigin: true in your option

$.post("https://login.salesforce.com/services/oauth2/token",
{
    grant_type:"password",
    dataType : 'jsonp',
    crossOrigin : true,   /// Add this option 
    headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"},
    client_id:"CLIENTID",
    client_secret:"CLIENTSECRET",
    username: "uname",
    password: "password"
},
function(data,status){
    //my_function(data);
    console.log(data);
});
function my_function(data){
    alert(data);
}