class ClickResponder implements ResponseTextHandler {
public void onCompletion(String responseText)
{
label.setText(responseText) ;
}
}
[...]
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if(!HTTPRequest.asyncPost(
"achilles.php?f=foo",
"x=15&zoo=bear",
new ClickResponder()))
{
label.setText("button POST request failed") ;
}
}
});
5. The relevant section of achilles.php is as follows:
if (isset($_GET['f']))
{
if ($_GET['f'] == 'foo')
{
print "foo(): " . print_r($_GET, true) . print_r($_POST, true) ;
}
}
6. After compiling and deploying everything to my webserver, I get the
following result in the label:
foo(): Array ( [f] => foo ) Array ( )
7. Clearly the GET data, embedded in the URL, makes it to the PHP
script and back to my application. It appears that no POST data is
detected by the PHP script. Why is this, and what should I be doing
differently?
public static boolean asyncFormPost(String url, String postData,
ResponseTextHandler handler) {
String moduleName = GWT.getModuleName();
return asyncFormPost(null, null, url, postData, handler,
moduleName);
}
public static native boolean asyncFormPost(String user, String pwd,
String url, String postData, ResponseTextHandler handler, String
moduleName) /*-{
var xmlHttp = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlHttp) {
alert('Cannot create XMLHTTP instance');
return false;
}
try {
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
if (moduleName) {
xmlHttp.setRequestHeader("X-GWTCallingModule", moduleName);
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(xmlHttp.responseText);
xmlHttp = null;
}
};
xmlHttp.send(postData);
return true;
}
catch (e) {
return false;
}
}-*/;
But in the meantime, there's an easy server-side fix if you're using a
PHP script. Just send the POST data like a URL-encoded string (for
example "foo=bar&x=y"), then use the PHP script to parse the raw POST
data manually into your $_POST global with the parse_str function.
So just add the following line at the top of your PHP script:
parse_str( $HTTP_RAW_POST_DATA, $GLOBALS['_POST'] );
Given the the above example, this function call would set $_POST[ 'foo'
] = 'bar' and $_POST[ 'x' ] = 'y'.
$_POST = $_GET;
?>
:)
That's my temporary fix untill asyncPost() with form encoding is
implemented.
It should be noted that use of $_GET as above will only work in simple
cases.
There is a limit (1k ??) for the length of the GET request string.
Cheers,
Aaron Watkins
----------------------
My Site: http://www.goannatravel.com