-1

Basically I am trying to get followers count of twitter using username by using this URL

https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=USERNAME

this url gives me json.json file. This file contains all the info like

[{
   "following":false,
   "id":"XXXXXXXXXXX",
   "screen_name":"XXXXXXXXXXXXX",
   "name":"XXXXXX XXXXXXXXX",
   "protected":false,
   "followers_count":10988,
  "formatted_followers_count":"10988 followers",
  "age_gated":false
}]

I am trying this code but not working. Please help.

function readTextFile(file, callback) {
    var rawFile = new XMLHttpRequest();
    rawFile.overrideMimeType("application/json");
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4 && rawFile.status == "200") {
            callback(rawFile.responseText);
        }
    }
    rawFile.send(null);
}

Usage:

readTextFile("https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=USERNAME", function(text){
    var data = JSON.parse(text);
    console.log(data);
});
Jamshaid Alam
  • 515
  • 1
  • 9
  • 24
  • 2
    Are you sure the server allows you to download the data? Because when I ran the code on my browser, I was blocked by the CORS policy. – Parvez M Robin May 16 '20 at 18:25
  • Yes I am getting this same error – Jamshaid Alam May 16 '20 at 18:28
  • 1
    Do you know what [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is? And why didn't you mention the error to us first?? "Not working" is a pointless description of a problem, it doesn't explain anything or give anyone any clues about what might be happening. Anyway if you have a CORS error it means that, unless you can get the agreement of the server owners to whitelist your website, you cannot fetch this data via AJAX. You'll need to use server-side code to make the HTTP request, and then display the data in your site. – ADyson May 16 '20 at 18:37
  • Ok, Understood. but what do you mean by server-side code? – Jamshaid Alam May 16 '20 at 18:43
  • Something which runs on your webserver, like PHP or ASP.NET or NodeJS (or many others). – ADyson May 16 '20 at 19:03

2 Answers2

0

As I mentioned in the comment, the code is okay. However, the site from which you are trying to fetch data does not allow CORS. That means the server only allows a particular number of websites to make a request to that server and you are not one of them. So, you are not able to get the data from the server unless the server allows you to.

Parvez M Robin
  • 154
  • 1
  • 3
  • 16
  • Ok, And what is the process of getting CORS agreement ? – Jamshaid Alam May 16 '20 at 18:46
  • 1
    There ain't any if you don't know who owns the server. – Parvez M Robin May 16 '20 at 19:04
  • @JamshaidAlam The process depends on the website. Some big API provider such as Google have a specific registration process where you sign in to their developer portal, and register your website, give details of the URL etc. And then they automatically add CORS headers for your site. But a smaller site may not have such a thing, you might have to contact the site owner by email or phone, and ask them. Of course, they may say "no, we don't want CORS requests coming to our site, it's a security risk". You'll have to contact them or read documentation to find out. – ADyson May 16 '20 at 19:05
0

So I found a solution, but i dont know if its the right way or not. Since I am using Firebase Hosting and firebase do not allow server code like PHP. so i wrote a php code and host it to other linux hosting and then I am calling php code using AJAX. Its working now. And yes I needed to add this as header in php

header('Access-Control-Allow-Origin: *'); 
Jamshaid Alam
  • 515
  • 1
  • 9
  • 24