views:

75

answers:

1

I'm trying to get cross-domain Ajax to work. I downloaded a PHP proxy script from the Yahoo Developer site, ran it from command line and verified that it receives the XML from the server with a GET request.

Now, I'm trying to connect to the PHP script within JS with no results. I have the following:

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
$.ajax({
    type:"GET", 
    url:"proxy.php", 
    dataType:"html", 
    success:function(msg){
        alert(msg);
    }
});
</script>

What this does, though, is to output the source of the PHP script in the alert box, not the XML! Where am I going wrong?

+1  A: 

Your server has to actually run php for this to work. From your question, it sounds like it's just serving up proxy.php as a text file.

You need to either use the server-side flavor of the proxy script that works for your platform, or also run/support php at the same time. I'd recommend the former, running an equivalent proxy in whatever language/platform you're already running server-side.

Nick Craver
+1 cause nick always get first to answer today :)
Reigel
@Nick, thanks for the answer. So, do I need to set up a server to run proxy.php on my client machine? I cannot run the script on the actual server that I'm trying to contact with JS. I'm hopelessly confused :-)
recipriversexclusion
OK, I installed Apache2 on my client machine (Ubuntu Karmic) and copied proxy.php to /var/www. Now, when I go to the URL http://localhost/proxy.php I can actually see the XML received from the remote server. Problem is, when I replace the line url:"proxy.php" above with url:"http://localhost/proxy.php", it doesn't display the received XML in an alert box, as it should but just displays it in a new page.
recipriversexclusion
@recipriversexclusion - Do you want your dataType to be `html`? It sounds like you're after something different here like `xml` or `json`.
Nick Craver