Is there a way to get data from an external URL with javascript?

Discussion in 'JavaScript' started by phantom, Nov 21, 2009.

  1. #1
    In php you can get the data from a URL with something like this

    $lines = file('http://mysite.com/info.php);
    
    foreach ($lines as $line) {
      echo "$line";
    }
    Code (markup):
    so I can get whatever info.php outputs...

    How can I do that with javascript?
     
    Last edited: Nov 21, 2009
    phantom, Nov 21, 2009 IP
  2. Freditpl

    Freditpl Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do it with JavaScript only, because browser is not allow cross site query.
     
    Freditpl, Nov 22, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    You can use an XMLHttpRequest to get the source of a page with javascript.. only if the requested page is on the same server as the page that the javascript is running on (this is commonly known as AJAX). Otherwise you will have to use a server-side script (run on your server) to return the requested (remote) page, kind of like a proxy, and only then you can use javascript to access the data retrieved.
    Some more info on XMLHTTPRequest: http://www.w3schools.com/XML/xml_http.asp
     
    camjohnson95, Nov 23, 2009 IP
  4. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Create a proxy on your server. Now create AJAX request to the proxy with URL you want.
     
    shubhamjain, Nov 24, 2009 IP