AJAX and external php

Discussion in 'JavaScript' started by degy, May 31, 2007.

  1. #1
    Hey guys i want to get data from external php file using AJAX (different domain name)

    var myRand2;
    myRand2=parseInt(Math.random()*99999);
    var url="http://www.thegirlz.net/chat/mainTXT.php?wadwij="+myRand2;
    xmlHttp2.onreadystatechange=stateChanged22
    xmlHttp2.open("GET",url,true)
    xmlHttp2.send(null)
    var objDiv = document.getElementById("mainTXT");
    Code (markup):
    just a part of my code i get error "permisson denied" when using this URL

    looks like browsers have default protection against requesting data from external files from JS code. But is there any way (like editing header sent) or anything that would allow me to override this?

    thanks for your answers. (i'll explain anything you don't understand (my english sux))
     
    degy, May 31, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I really don't think so

    From what I know AJAX isn't cross-domain
     
    decepti0n, May 31, 2007 IP
  3. 3l3ctr1c

    3l3ctr1c Peon

    Messages:
    380
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Make a PHP file to get the data from the "external PHP" and link it to your ajax script, that should do the job.
     
    3l3ctr1c, May 31, 2007 IP
  4. degy

    degy Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yeah i was thinking about that too. But i'm actualy working on a script that will be remote hosted so users will only have to include a single line of code into their site.

    Everything else loads from my site. So that is not an option (and it's probably the only way it could work :p ).
     
    degy, Jun 1, 2007 IP
  5. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When I need to contact a remote URL with AJAX, I use the following XML_Proxy.php

    
    <?php
    	header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
    	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    	header("Cache-Control: no-store, no-cache, must-revalidate");
    	header("Cache-Control: post-check=0, pre-check=0", false);
    	header("Pragma: no-cache");
    	$url = "http://www.weather.gov/data/current_obs/";
    	$url .= $_GET['code'];
    	$session = curl_init($url);
    	curl_setopt($session, CURLOPT_HEADER, false);
    	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    	$xml = curl_exec($session);
    	header("Content-Type: text/xml");
    	echo $xml;
    	curl_close($session);
    ?>
    
    Code (markup):
    The AJAX function contacts XML_Proxy.php, and the proxy file contacts the remote URL. The remote URL responds to the proxy, the proxy responds to the AJAX function.
     
    Mike H., Jun 1, 2007 IP
  6. degy

    degy Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    well thanks for your replays i'll go with this proxy script for now. We'll see how it goes. I'll probably have to find some other solution but it'll work for now ;)
     
    degy, Jun 1, 2007 IP