0

XMLHttpRequest works for first url but it doesn't work for second url.I think this function doesn't work for dynamic web pages.I also tried ajax to get html source but it didn't work too.What can I do?How can I change this code to work for second url?

<script language="Javascript" type="text/javascript">

function getSource(url)
{
    var req = new XMLHttpRequest();
    req.open(
        "GET",
        url,
        true);
    req.onreadystatechange = statusListener;
    req.send(null);
    function statusListener()
    {
    if (req.readyState == 4) 
        {
            if (req.status == 200)
            {
                var doc=req.responseText;
                alert(doc);
            }
        }
    }
}

url1 = "https://pages.github.com/";
url2 = "https://stackoverflow.com/";

// This code WORKS
getSource(url1);

// This code DON'T WORK
getSource(url2);
</script>
  • Error message tells you why.... CORS – epascarello Oct 25 '18 at 17:56
  • Thanks.But I need to get html source code from some URLs in client side despite CORS.What can I do? – curious_coder Oct 25 '18 at 18:07
  • Well if you do not have the key you can not get in the door so it is not possible to do it with just clientside code. You would need to use a proxy on a serverside to read it. – epascarello Oct 25 '18 at 18:09
  • 1
    Probably not really much as CORS needs to be set on the server-side, in this case stackoverflow. Do you really need the html code from stackoverflow or do you need infomation from the website. If it's info i woudl checkout https://api.stackexchange.com/ – relief.melone Oct 25 '18 at 18:10

1 Answers1

0

I ran your code in a JSFiddle, and I got an error message saying it is a CORS issue. You can view these error messages by opening the Javascript console in your browser of choice.

Error message as seen in the Javascript console

CORS (Cross-Origin Resources Sharing) issues arise when you don't have the sufficient permission to access resources between different domains. Each web server has policies set up that determine the rules for accessing these files, and Stack Overflow's security settings are set up to disallow that kind of access to their site. You can read more about CORS here.

Oztaco
  • 3,399
  • 11
  • 45
  • 84
  • Thanks.Are there any ways to get pages' html source from urls in client side? – curious_coder Oct 25 '18 at 18:10
  • @curious_coder The method you used works fine for most pages, it only does not work if you don't have permission to do that. And since the web server sets those rules, there is nothing you can do client-side to change that. The only way you can get past it is if you have your own web server, and you that web server make requests for you and act as a proxy. Apparently, there is a service that does this for you ( https://medium.com/netscape/hacking-it-out-when-cors-wont-let-you-be-great-35f6206cc646 ) but be careful because you don't want to use data that you aren't supposed to have maliciously – Oztaco Oct 25 '18 at 18:15