views:

860

answers:

6

Hello,
I am using this AJAX proxy for cross-domain AJAX requests: http://www.daniweb.com/code/snippet494.html. It works great when I use GET. But when I try to use POST, the body seems to get lost and is never printed out. I tried printing the log and studied the code but I cannot figure out what is going wrong.
Thanks,
Isaac Waller

Edit: Here is the code I am using:

$.post("proxy.php?proxy_url=http://www.google.com", { postvar1: "hello", postvar2: "goodbye" },
  function(data){
    alert(data);
  },"text");

Even if I make a simple form and test it with that no output results.

A: 

please post the html/javascript on your page that is failing.

The script you referenced expects parameters in the query string, $_GET, and optionally parameters in post which it merely passes on.

Unless you modified that script, it will not work unless you have the information in the query string.

You can however have both get and post in the same request.

e.g.

var http = new XMLHttpRequest();
var url = "http://example.com/proxy.php?proxy_url=http://www.google.com";
var params="postvar1=hello&postvar2=goodbye";
http.open("post", url, true);
http.onreadystatechange = function() {
    alert("finished");
}
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);

notice the url used includes query string parameters, AND uses post with parameters

or in jQuery:

$.post("proxy.php?proxy_url=http://www.google.com", { postvar1: "hello", postvar2: "goodbye" },
  function(data){
    alert(data);
  },"text");

or in Prototype:

 var http = new Ajax.Request("proxy.php?proxy_url=http://google.com", 
     { method: "post", 
       parameters: { postvar1:"hello", postvar2: "goodybe"}, 
       onSuccess: function(text) { alert(text);}
     });
Jonathan Fingland
A: 

Do not take my word, but you can't do cross-domain Ajax. There are workarounds as described in this article, but pure ajax is not possible.

usoban
I know, I am using a proxy, that's what the question is about.
Isaac Waller
+3  A: 

Try this:

$.post("/proxy.php", {proxy_url: "http://www.google.com", postvar1: "hello", postvar2: "goodbye" },
  function(data){
    alert(data);
  },"text");

When I tried your original version, it came back with an error 405, but the above works. Either JQUERY or proxy.php isn't happy about mixing post and get parameters.

EDIT:

I did get back a page, which I was able to display in the alert box, but it doesn't look anything like what I'd expect. I tried the same code on some sites with contact forms that post, and it worked as expected.

Lucky
A: 

There might be something wrong with the proxy.php script, so that it only returns something when using GET. Maybe you get the request data from $_GET instead of from $_REQUEST in the php script? Or maybe Google just doesn't answer POST requests. You might want to translate POSTs to GETs in that case.

Off-topic: be careful not to be a proxy for everything on the web, if all you want is to proxy for a couple of trusted domains.

lbp
A: 

Change this line...

$proxy_url = isset($_GET['proxy_url'])?$_GET['proxy_url']:false;

To this...

$proxy_url = isset($_GET['proxy_url']) ? $_GET['proxy_url'] : (isset($_POST['proxy_url']) ? $_POST['proxy_url'] : false);

And that will fix the server-side issue.

However, when you make the request via jQuery, you need to pick either GET or POST. Currently, you are using a combination of both by passing proxy URL via query string in addition to posting parameters. This will fail, but it is easy to fix...

$.post("/proxy.php", {proxy_url: "http://www.google.com", foo: "bar" }, function(dat) {
  alert(dat);
});
Josh Stodola
+1  A: 

Replace any and all $_GET variables in your PHP file with $_REQUEST.

Don Wilson