Get content from a page using Xmlhttprequest

Asked

Viewed 127 times

0

I am using Xmlhttprequest to get content from a page. I have the code that works to create the object and send a alert. I wanted to get the contents of a page in mode assync.

This is the code creating the object:

var xhr_object = null;

    if(window.XMLHttpRequest) // Firefox
       xhr_object = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { /
       alert("Nao suporta XMLHTTPRequest...");
       return;
    }

    //methodo de transmicao
    //url
    // true mode asynchrone
    xhr_object.open("GET", "http://page.htm", true);

    //4 (complete) 
    xhr_object.onreadystatechange = function() { 
        if(xhr_object.readyState == 4) alert("Request!");
    }

    //executar request
    xhr_object.send(null);
  • What’s the problem? Do you have Alert? No? Do you get dates? Idea: puts an "Else" in the test on xhr_object.readyState == 4 or makes an Alert of the readyState result.

  • I use Alert(xhr_object.responseText) but it does not show the page date.

  • I’m talking about doing Alert(xhr_object.readyState) to read the server response. I think that’s the first step. Also check the URL. Because I think "http://page.html" is a little weird... :)

  • I get the value 1, sure I only took the ip from the server.

  • Conform value in W3C: 0: request not initialized 1: server Connection established 2: request Received 3: Processing request 4: request finished and Response is ready If you want to read a "local" page, put only page.html without http. Or put the full address with IP the domain name.

  • The connection is established, I can not show the content of the link.

  • 2

    Are you respecting the same origin policy?

  • The request is finished (4) and still shows nothing from Alert(xhr_object.responseText); and I changed the url xhr_object.open("GET", "ip/date.php", true);

  • Take a look at the Cahe comment. Where is your ip/date.php page? On the same server as the page making the call?

  • Try to wrap the code with the try/catch and checks which error is displayed

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.