SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XMLHttpRequest - Access is Denied

    Hi all,

    I'm trying to access my server from another server, using AJAX, which I know is automatically denied to prevent other people from getting information, but how do I unblock this, or in other words, get around this so I can access this information on my other server? Thanks, all

    Below is my code that i'm currently using to get the data:

    Code:
    var xmlHttp; var xmlStr;
    
    function GetXmlHttpObject(){
     var xmlHttp=null;
      try{// Firefox, Opera 8.0+, Safari
       xmlHttp=new XMLHttpRequest();
      } catch (e){//Internet Explorer
      try{
       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e){
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    
    function stateChanged() { 
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
      xmlStr=xmlHttp.responseText;
     } 
    }
    
    function loadXMLFile(){
     var xmlFile="http://www.tnpfk.com/includes/ai/ai_weather.php?city="+document.getElementById('city').value+
      "&country="+document.getElementById('country').value;	
       xmlHttp=GetXmlHttpObject();
        if(xmlHttp==null){alert("Browser does not support HTTP Request");return;}
       xmlHttp.onreadystatechange=stateChanged;
       xmlHttp.open("GET",xmlFile,true);
       xmlHttp.send(null);
    }

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    One solution might be to write some kind of script (PHP,ASP,etc) to read the external source and make the AJAX-call to your local script.
    The script will "tunnel" the other site to your site.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No can do. http://www.google.co.uk/search?q=cro...xmlhttprequest
    It'll probably be possible at some point in the future, but not today. Your only solution to this problem is to use a hidden iframe, which is unpleasant and not recommendable. Here is an example. It would also be liable to break as you would have to load the iframe, access its contents and either parse the innerHTML with regular expressions or walk the document in the iframe using the DOM. Both methods are fragile because the owner of the page you're requesting can change it without your knowledge.

    As scallioXTX says, you can also point to a script on your server to do this, but then you have two independent processes which can fail, which means your request for the page is twice as likely to fail. It also makes your server do twice as much work, which can in the end be expensive.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that sucks, I was hoping to create an AJAX Weather RSS Feed--I geuss i'll just stick with reloading with PHP for now until something like that is supported.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you not do this when the page first loads? Or does it depend on user input?

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I'm guessing you'll be waiting a very long time then.
    I don't think it will ever be implemented. The security issues are to big to allow for it.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It requires user input, and the data is kind of lengthy (that is returned).

    edited.
    Last edited by Enjoii; Aug 18, 2008 at 16:45. Reason: Missing Information

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The way to do an Ajax request to another server on a different domain is as follows.

    1. JavaScript makes Ajax request to loacal server (same domain)
    2. Script on local server passes request to remote server.
    3. Remote server processes result and returns it to local server.
    4. Local server passes the response back to the page.

    The only difference from a local call is that you need an extra script on the local server to pass the request to and from the other domain.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The way to do an Ajax request to another server on a different domain is as follows.

    1. JavaScript makes Ajax request to loacal server (same domain)
    2. Script on local server passes request to remote server.
    3. Remote server processes result and returns it to local server.
    4. Local server passes the response back to the page.

    The only difference from a local call is that you need an extra script on the local server to pass the request to and from the other domain.
    Correct me if I'm wrong, but IIRC, that "other" domain will still need to give you the data your server script is asking for (and is prepared to parse). However, if you're doing it guerilla-style, and not telling that other server and that you're effectively "data scraping", once they change their page, your entire setup is... well... kaput.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use IFrame.

  12. #12
    SitePoint Wizard jimbo_dk's Avatar
    Join Date
    May 2005
    Location
    Singapore
    Posts
    1,261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XLCowBoy View Post
    Correct me if I'm wrong, but IIRC, that "other" domain will still need to give you the data your server script is asking for (and is prepared to parse). However, if you're doing it guerilla-style, and not telling that other server and that you're effectively "data scraping", once they change their page, your entire setup is... well... kaput.
    This problem would still exists if cross-domain XMLHTTPRequests were allowed. For most uses, there is no need for the server to parse any code, but just return the code back to the client.
    Winners Respond. Losers React.
    Singapore Web Designer

  13. #13
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by firebladeiii View Post
    Use IFrame.
    That will not work because JavaScript can't access anything from the iframe when it is a different domain.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  14. #14
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimbo_dk View Post
    This problem would still exists if cross-domain XMLHTTPRequests were allowed. For most uses, there is no need for the server to parse any code, but just return the code back to the client.
    Not quite sure what you mean by that.


    We ourselves are working on a site that "calls" product feeds from a feed provider - they give us an XML feed, and our PHP handles the "parsing" into the database. There is a faster way, which is by using their API, but we wanted a bit more control, so we built our own.

    This is why I do believe my knowledge is correct: I can't see any stable way to constantly access data unless the data structure is "fixed". I mean, even Google needs a bit of <title>,<h1>,<meta> for their spiders. I would assume that AJAX / Remote Server calls are just as particular.

  15. #15
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why can't you have an RSS AJAX weather feed? Use something like http://code.google.com/apis/ajaxfeeds/

    That way you don't have to worry about http tunnelling, I imagine integrating an api into your application is going to be far more streamlined than trying to re-invent the wheel.

  16. #16
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is possible, and I've done it before for a web stats script. The trick is that we won't be using the XmlHttpRequest method at all, and we have to control the server (or hosting account) at the destination (the site you want to perform the XmlHttpRequest on).

    1. Call a standard <script src=""> tag with the src="" portion including a script from the other domain. Something like "http: //example.com/phpjs/haha-this-is-really-php.js?action=weather&element=weatherDiv"
    2. The other server will have a PHP file (or some other server-side code) that will actually be loaded for that request using mod_rewrite.
    3. That script will do what it needs to do, and then return ONLY javascript. That javascript will then be executed by the <script> tag in step #1 by the website that included it from another domain.
    4. Remove the javascript after execution with DOM scripting, and include it (or another one) again to simulate additional AJAX calls.

    So it's more like - This end goal is possible, just without true "AJAX", and through a roundabout way by including remote javascript files (you can do this dynamically with some DOM scripting) that are put together by a server-side language.

  17. #17
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bit of an ugly solution though...could also run into caching problems. I think tunnelling is the most sustainable option in this scenario, or using an API like Google's to handle the grunt work for you.

  18. #18
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While driving home from college last night I thought of the same thing felgal came up withl. I setup a PHP script that went to another PHP script on my server. Then I used XMLHttpRequest to return the server-script, which got the information, but it *does* take a little longer to retrieve the data. I made sure it worked in Opera, FF, Safari, and IE7, which all worked, but in any case it now works.
    Last edited by Enjoii; Aug 19, 2008 at 08:20. Reason: wrong info

  19. #19
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a technique specifically for this: JSON with padding. jQuery supports it:

    http://docs.jquery.com/Release:jQuery_1.2/Ajax

    Or you can follow this simple example to write your own:

    http://www.adspeed.org/2008/08/json-...ing-jsonp.html
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  20. #20
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Their is a way to do this but you need a proxy server like Apache and do proxy pass as below

    ProxyPass /mirror/foo/ http://www.microsoft.com/

    This probably the most cleanest way of doing things. Other solution is iFrame/Frame solutions, which works.

  21. #21
    SitePoint Zealot ShytKicka's Avatar
    Join Date
    Aug 2004
    Location
    New York
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about using something like mod-rewrite? Not sure if that will work, but could be worth a try.

    If that doesn't work, call a PHP page on your server which does an HTTP request to the other server. That would get the data and return it to the PHP page, and the PHP page would return it to the AJAX script.


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
  •