is HTTPRequest.asyncPost() implemented?

9 views
Skip to first unread message

jamesm

unread,
May 20, 2006, 3:52:58 PM5/20/06
to Google Web Toolkit
1. I am building a GWT front end for an existing PHP+MySQL application
2. Calls to HTTPRequest.asyncGet() are generally successful
3. Calls to HTTPRequest.asyncPost() never seem to send any POST data
4. My GWT code is as follows:

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?

Message has been deleted

Galmar

unread,
May 31, 2006, 5:53:52 AM5/31/06
to Google Web Toolkit
The implemented asyncPost uses text content-type and not URL encoded
content-type as you are trying to post.
Here's a code that implement a url encoding post (hopefully, Google
will supply this feature soon)

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;
}
}-*/;

Mr. Monster

unread,
May 31, 2006, 9:54:53 AM5/31/06
to Google Web Toolkit
Bump my post
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/37eef6eaf3de80a8
to hopefuly get some attention to this issue, it definetly needs to get
addressed.

Santos

unread,
Jun 5, 2006, 2:44:36 PM6/5/06
to Google Web Toolkit
Galmar's right, let's hope that the GWT developers put some
url-encoding functionality into this class.

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'.

Mr. Monster

unread,
Jun 5, 2006, 3:01:44 PM6/5/06
to Google Web Toolkit
<?PHP

$_POST = $_GET;

?>

:)

That's my temporary fix untill asyncPost() with form encoding is
implemented.

Azzah

unread,
Jun 6, 2006, 2:51:53 PM6/6/06
to Google Web Toolkit
Hi,

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

Reply all
Reply to author
Forward
0 new messages