当我使用Ajax进行GET时,我在控制台屏幕上出现如下错误:
无法加载https://www.doviz.com/api/v1/golds/all/latest:对预检请求的响应未通过访问控制检查:“Access-Control-Allow-Origin”标头的值“https://www.doviz.com”不等于提供的原产地。因此,不允许原点“http://www.elli2.com”访问。
但是当我从网络选项卡中检查地址时,我发现数据来自数据。
代码片段如下所示:
$.ajax({
dataType : "json",
url : "https://www.doviz.com/api/v1/golds/all/latest",
headers: {
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Methods":'POST,GET,PUT,DELETE',
"Access-Control-Allow-Headers":'Authorization, Lang'
},
success : function(data){
var ALTIN = data[5];
var CHANGE_RATE = Number(ALTIN["change_rate"]).toFixed(2);
document.getElementById("Altin").innerHTML = Number(ALTIN["buying"]).toFixed(2);
document.getElementById("AltinOran").innerHTML = CHANGE_RATE > 0 ? "+ %"+CHANGE_RATE : "- %" + CHANGE_RATE;
document.getElementById("ALTINICON").classList = CHANGE_RATE > 0 ? "fa fa-caret-up" : "fa fa-caret-down";
document.getElementById("ALTINICON").style.color = CHANGE_RATE > 0 ? "green" : "red";
document.getElementById("ALTINICON").style.fontSize = "20px";
}
})