Salesforce 响应中的跨源问题(Access-Control-Allow-Origin)

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

我尝试使用 JS 从外部本地文件中获取来自 salesforce 的记录。 我可以在网络选项卡中看到响应。 我在控制台中收到一条错误消息:

"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."

我的代码:

$.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);
}

任何帮助和建议。

在您的选项中添加crossOrigin: true

$.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);
}