Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS error with ajax request [duplicate]

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);
          }
        })
    }
like image 258
xenurs Avatar asked Apr 12 '16 09:04

xenurs


1 Answers

You could get around this by using jsonp. Change dataType to jsonp so your GET request should 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);
          }
      })
}
like image 164
Paul Fitzgerald Avatar answered Nov 14 '22 20:11

Paul Fitzgerald