Closed Thread
Results 1 to 5 of 5

Thread: Fetching google.com with AJAX?

  1. #1
    TriKri Guest

    Fetching google.com with AJAX?

    I have written a script which is suposed to load the google site with ajax and then write it to the html, so that the google page will load (but with my adress!). This is the code:

    Code:
    <html>
      <head>
      <title>Fetch URL</title>
        <script type="text/javascript">
          function FetchURL() {
            
            var xmlHttp;
            
            try { // Firefox, Opera 8.0+, Safari
              xmlHttp=new XMLHttpRequest();
            }
            catch (e) { // Internet Explorer
              try {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e) {
                try {
                  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                  alert("Your browser does not support AJAX!\nTo bad.");
                  document.write("FetchURL won't work.");
                }
              }
            }
            
            xmlHttp.onreadystatechange=function() {
              if(xmlHttp.readyState==4) {
                if (xmlHttp.responseText.length)
                {
                  alert("given URL has a length of " + xmlHttp.responseText.length);
                  alert(xmlHttp.responseText);
                  document.write(xmlHttp.responseText);
                }
                else {
                  alert("given URL is empty.");
                }
              }
            }
    
            var URL = "www.google.com";
            xmlHttp.open("GET", URL, true);
            xmlHttp.send(null);
          } //FetchURL()
        </script>
      <head>
      <body onload="FetchURL();">
      </body>
    </html>
    But it doesn't work!

    I saw I got the following error message from IE6: The requested URL /www.google.com was not found on this server. Not strange, I don't have google at my server! But I thought the intention with the XMLHttpRequest object was that one can do a request which is not from the server but from the clients computer (to any server and url)?

  2. CODECALL Circuit advertisement

     
  3. #2
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0
    Just a guess, but do you need the http:// to tell it to not look on your server?
    So you would change:
    Code:
    var URL = "www.google.com";
    to:
    Code:
    var URL = "http://www.google.com";

  4. #3
    oubless is offline Newbie
    Join Date
    May 2007
    Posts
    22
    Rep Power
    0
    Quote Originally Posted by CygnetGames View Post
    Just a guess, but do you need the http:// to tell it to not look on your server?
    So you would change:
    Code:
    var URL = "www.google.com";
    to:
    Code:
    var URL = "http://www.google.com";
    not sure this will work either ...
    I think for security purposes it is stopped. AJAX works on the same domain only ( or at least when I tried to use xmlHttpRequest on other domain it failed with a security reason ). Using it with other domains allows scripting between different domains which is considered security hole.
    Again - not 100% sure about that, but I think it is so! scripting between different domains is supposed be forbidden even when using pop-up, frames and iframes.

    read this one, might be helpful:
    Cross-Domain Proxy - Ajax Patterns

  5. #4
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0
    Very interesting. This is probably why some sites are producing publicly available APIs for people to use.

    TriKri: It might be worth looking at google's APIs to see if what you want to do is possible using them.

  6. #5
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0
    oubless is correct. Most browsers do not allow you to access different domains using XHR for security purposes. Even if you could, I'm fairly certain that the folks at Google wouldn't be very happy about what you're trying to do. If you are interested in having a Google search on your website check this link out.

    Also, you should consider using prototype.js which I personally highly recommend or another JavaScript library. Prototype and many other libraries provide a much more programmer friendly interface for crating AJAX requests...its much easier and cleaner than using XHR yourself.

    Happy Coding.
    -Fahlyn
    Visit My Google Group Here: Web Development Innovation

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Google AJAX Search? New?
    By DEViANT in forum The Lounge
    Replies: 6
    Last Post: 10-21-2010, 09:34 PM
  2. External Download Fetching
    By thorax232 in forum Python
    Replies: 0
    Last Post: 06-14-2010, 11:10 AM
  3. PHP Fetching
    By Overload in forum PHP Development
    Replies: 2
    Last Post: 11-16-2009, 08:17 AM
  4. Fetching google.com with AJAX?
    By TriKri in forum JavaScript and CSS
    Replies: 2
    Last Post: 09-03-2007, 07:34 AM
  5. Google AJAX Resource
    By NeedHelp in forum JavaScript and CSS
    Replies: 2
    Last Post: 07-06-2006, 04:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts