ajax请求出现CORS错误
CORS error with ajax request
我在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);
}
})
}
相关文章:
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- 在Polymer中使用核心ajax时出现CORS错误
- 在 AngularJS 和 CodeIgniter3 中开机自检数据时出现 CORS 错误
- AngularJS应用程序中的CORS错误
- Meteor:使用HTTP.get从客户端访问网站(CORS错误)
- Catching'阻止加载混合活动内容'CORS错误
- ajax请求出现CORS错误
- 加载模板时出现AngularJS、Localhost和CORS错误
- 谷歌加 CORS 错误
- 同一域上的 CORS 错误
- 使用 elasticsearch.js 时出现 CORS 错误
- 护照节点.js CORS 错误
- 简单请求中的 Cors 错误:请求标头字段 预检响应中的访问控制-允许-标头不允许授权
- Firefox 和 chrome 的 CORS 错误
- 重定向时出现机架 CORS 错误
- 从localhost使用谷歌联系人api创建联系人时出现CORS错误
- Facebook oEmbed端点CORS错误
- 在Angular中使用解析客户端密钥会导致CORS错误
- 从文件//scheme运行的应用程序出现CORS错误