0

I am writing an script to get my twitter json data. I did everything correctly and got bearer token form hurl.it. But it is producing error when I am using that bearer token to get twitter data. What is wrong with my code. It is throughing the following error.

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

My code is:

$(function(){
  $.ajax({
    type: 'GET',
    url: 'https://api.twitter.com/1.1/users/show.json?screen_name=menightfury',
    headers: {
      'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAGEkywAAAAAA%2F81O13D%2B6ZDrS%2FMfOBYOBKrBygg%3DBEMX87IsIhpgH677fGhH7BcARa6XwnZHpal94fYcTo3thsbWRO'
    },
    success: function(data){
      $('body').append(JSON.stringify(data));
    },
    error: function(err){
      $('body').append(JSON.stringify(err));
    }
  });
});
  • Check the console. You'll see an error saying: `No 'Access-Control-Allow-Origin' header is present on the requested resource`, eg https://jsfiddle.net/vnL41n9f/. This is because the endpoint you're calling does not add CORS headers to the response. Hence you cannot make a call to it through JS code due to the Same Origin Policy. You will have to make the request server side instead. See the question I marked as duplicate for more information. – Rory McCrossan Jan 25 '17 at 11:29
  • Also note that Twitter have a JS SDK which may be better suited to your needs: https://dev.twitter.com/web/javascript – Rory McCrossan Jan 25 '17 at 11:30
  • Can anyone please describe what the appropriate code would be ?/ – meNight Fury Jan 25 '17 at 12:25
  • There isnt any. You either need to write server side code or use rhe twitter sdk – Rory McCrossan Jan 25 '17 at 12:35

0 Answers0