-3

in a webpage i would like to collect a response from another web server at a given URL address.

let's say someone else has a server at http://mysite/123 that responds with a simple string. (without headers and stuff).

what is the most SIMPLE way to get javascript on my webpage to collect a url's raw response in preferably a byte array variable? though i would except an answer that saves in string to get me going. this is an exact copy paste from my html document and its not working for me.

thanks!

<script>

var txt = "";

txt=httpGet("https://www.google.com");
alert(txt.length.toString());

function httpGet(theUrl) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false);
    xmlhttp.send();
}
</script>
TaterKing
  • 75
  • 1
  • 8

1 Answers1

-1

So I'd have to say your best bet would be to look into making an HTTP (or XHR) request from javascript. check: Return HTML content as a string, given URL. Javascript Function

function httpGet(theUrl)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false );
    xmlhttp.send();    
}
Community
  • 1
  • 1
STiTCHiCKED
  • 506
  • 1
  • 5
  • 18
  • i added a copy/paste of exactly what i pasted into a brand new html document in the question ... im not getting anything. any idea what i am doing wrong? – TaterKing Dec 21 '16 at 02:32