我在ajax中执行这个请求,但我仍然有以下关于CORS的错误:XMLHttpRequest无法加载https://cubber.zendesk.com/api/v2/organizations/37520251/users.json.飞行前响应中的访问控制允许标头不允许请求标头字段访问控制允许来源。你能帮我吗?(我已经看过很多话题了,我仍然不明白为什么不起作用
function afficheorga(a){
$.ajax({
url: "https://cubber.zendesk.com/api/v2/users/"+a+"/organizations.json",
type: 'GET',
dataType: 'json',
cors: true ,
contentType:'application/json',
secure: true,
headers: {
'Access-Control-Allow-Origin': '*',
},
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
},
success: function (data){
console.log(data.organizations[0].name);
var organisation = data.organizations[0].name;
$("#company").text(organisation);
}
})
}
您可以通过使用jsonp
来解决此问题。将dataType
更改为jsonp
,因此您的GET
请求应如下所示
function afficheorga(a){
$.ajax({
url: "https://cubber.zendesk.com/api/v2/users/"+a+"/organizations.json",
type: 'GET',
dataType: 'jsonp',
cors: true ,
contentType:'application/json',
secure: true,
headers: {
'Access-Control-Allow-Origin': '*',
},
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
},
success: function (data){
console.log(data.organizations[0].name);
var organisation = data.organizations[0].name;
$("#company").text(organisation);
}
})
}