response.getStatusCode()

295 views
Skip to first unread message

Dee Ayy

unread,
Sep 10, 2012, 3:13:54 PM9/10/12
to google-we...@googlegroups.com
I'm following https://developers.google.com/web-toolkit/doc/latest/DevGuideServerCommunication#DevGuideHttpRequests

I have
String url = "http://.../index.php";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
URL.encode(url));

try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}

@Override
public void onResponseReceived(Request request, Response response) {
Window.alert("getStatusCode:" + response.getStatusCode());
if (200 == response.getStatusCode()) {
// Process the response in
// response.getText()
Window.alert("RESPONSE:" + response.getText());
} else {
// Handle the error. Can get the status
// text from response.getStatusText()
Window.alert("OTHER STATUS:" + response.getStatusText());
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}

I get an alert box from the above code "getStatusCode:0" although my
Apache2 access logs has code 200 when this code is hit.

(And then "OTHER STATUS:" with empty status text).

A comment in stackoverflow says, "We've found a status code of "0"
usually means the user navigated to a different page before the AJAX
call completed."

Going directly to the URL in a browser returns a test JSON string
[{"symbol":"ABC","price":40.485578668179,"change":-0.53944918844604},
{"symbol":"DEF","price":1.3606576154209,"change":0.0051755221198266}]

Please advise.

Dee Ayy

unread,
Sep 10, 2012, 3:40:09 PM9/10/12
to google-we...@googlegroups.com
SOLVED
I assume this is due to the Same Origin Policy.

When I put the compiled GWT code on the production server, which is
the same URL in my question below, it works fine.

I had been testing this code locally (on the "development server"),
but making a URL request to the remote server (the "production
server").

I suppose I would have to truly mirror development and production
environments to test locally on the "development server" to not see
this error, and still use GWT debugging, rather than only testing
compiled code.

Thomas Broyer

unread,
Sep 10, 2012, 5:07:28 PM9/10/12
to google-we...@googlegroups.com


On Monday, September 10, 2012 9:40:44 PM UTC+2, gwtApprentice wrote:
SOLVED
I assume this is due to the Same Origin Policy.

Right.
 
When I put the compiled GWT code on the production server, which is
the same URL in my question below, it works fine.

I had been testing this code locally (on the "development server"),
but making a URL request to the remote server (the "production
server").

I suppose I would have to truly mirror development and production
environments to test locally on the "development server" to not see
this error, and still use GWT debugging, rather than only testing
compiled code.

Reply all
Reply to author
Forward
0 new messages