带有ajax请求的CORS错误 [英] CORS error with ajax request

查看:2160
本文介绍了带有ajax请求的CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ajax中执行此请求,但我仍然有以下关于CORS的错误:
XMLHttpRequest无法加载 https://cubber.zendesk.com/api/v2/organizations/37520251/users.json 。请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin。
你能帮帮我吗(我见过很多话题,但我仍然不明白为什么不能正常工作

i'm doing this request in ajax but i still have this following error about CORS: XMLHttpRequest cannot load https://cubber.zendesk.com/api/v2/organizations/37520251/users.json. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response. Can you help me pls ( i have seen many topic and i still don't understand why it is not working

 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 请求为跟随

You could get around this by using jsonp. Change dataType to jsonp so your GET request to be as follows

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

这篇关于带有ajax请求的CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆