3

I'm new in gwt and new to using Firebug. MY gwt version is 2.0.0. using eclipse and WAMP. my IIS is stoped to run WAMP apache. I run my program on firefox
I have valid json result from tesdb3.php located in "http://localhost/phpmyadmin/tesdb3/datauser.php"

{"item": [{"kode":"002","nama":"bambang gentolet"},
          {"kode":"012","nama":"Algiz"}]}

I add the xml with

<inherits name='com.google.gwt.json.JSON'/>
<inherits name="com.google.gwt.http.HTTP" />

then I try to show it in the gwt with this code.

public class Tesdb3 implements EntryPoint { 

String url= "http://localhost/phpmyadmin/tesdb3/datauser.php";

public void LoadData() throws RequestException{             

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

    builder.sendRequest(null, new RequestCallback(){
        @Override
        public void onError(Request request, Throwable exception) {
            Window.alert("error " + exception);
        }
        public void onResponseReceived(Request request,
                Response response) {
              if (200 == response.getStatusCode()) {
                  Window.alert("ok -" + response.getText() + "-" + response.getStatusCode());
              } else {
                  Window.alert("error2 -" + response.getText()+ response.getStatusText() + "-" + response.getStatusCode());
              }         
        }
    });
}

public void onModuleLoad() {        
    try {
        LoadData();
    } catch (RequestException e) {
        e.printStackTrace();
    }       
}
}

I run it in development mode. not hosted mode.
My code didn't show any error. But the result in window alert is "error2 --OK-0".

result Net from firebug is 7 request:
get Tesdb3.html?gwt.codeserv = 200ok
get Tesdb3.css = 200ok
get tesdb3.nocache.js = 200ok
get hosted.html?tesdb3 = aborted
get standard.css = 304 not modified
get hosted.html?tesdb3 = 403 not modified
get datauser.php = 200ok

My question is:

Why the response status code is 0, and the response status text is 'OK'? there was no error in json or Java code.

Why response.getText is empty? Why I can't get any json result even a single character?

Please help me. already 2 months I try to solve this with many source type and I can't get a single result.

This is my datauser.php

  header('Content-type: application/json; charset=UTF-8');
  header('Cache-Control: no-cache');
  header('Pragma: no-cache');

  $link = mysql_connect("localhost", "root", "")
        or die("Could not connect : " . mysql_error());
  mysql_select_db("tesku1") or die("Could not select database" . mysql_error());

  $query = "select * from tabel1";
  $result = mysql_query($query);

  $jumlah_data = mysql_num_rows($result);

  echo '[';

  for ($i=0; $i<=count($result); $i++){
      $row = mysql_fetch_array($result);

      echo '{';
      echo "\"kode\":\"$row[kode]\",";
      echo "\"nama\":\"$row[nama]\"";

      if ($i==count($result)){
       echo '}';
      }else
      echo '},';
  }
  echo ']';

  mysql_free_result($result);
8
  • Can you run the request through a proxy, or use firebug, to tell us exactly what the response is? Commented Jun 7, 2010 at 13:04
  • Are you setting the correct headers for your JSON output? header('Content-type: application/json; charset=UTF-8'); And what about other status types? Like, does 404s or 500s "get through" or also result in status code 0? Commented Jun 7, 2010 at 13:18
  • @tdavies: He already posted the results from the Net tab from Firebug. Commented Jun 7, 2010 at 13:19
  • @igor -- sure, but I'd like to see exactly what the response body and headers contained Commented Jun 7, 2010 at 14:14
  • header('Content-Type: text/javascript'); header('Cache-Control: no-cache'); header('Pragma: no-cache'); thats my header. This one i get from an example. I try with header('Content-type: application/json; charset=UTF-8'); the result is same. Commented Jun 7, 2010 at 15:18

3 Answers 3

1

I know this is an old post, but I intend to post an answer to all who are having this problem currently.

The cause of this problem is SOP (Same Origin Policy). This problem arises from the fact that PHP script is not in the same domain as your GWT or JavaScript web application.

The solution is quite simple just add a new header to your PHP script like this:

header('Access-Control-Allow-Origin: *');

this will tell GWT that the domain (site) from where the php script runs accepts requests from any other domain (sites).

To restrict request to a given site just add the following header:

header('Access-Control-Allow-Origin: http://mysite.com');

Where a java script from http://mysite.com makes the http request.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, looks like the problem is SOP(Same Origin Policy), and cross site request. From what I get(not detailed though) if the request is cross-site, RequestBuilder can't be used.
For exchange, use getJson() and JSNI overlay types. All example from this tutorial : http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html. I change the price value into my database value. At the end..my database showing up in my browser(yeah!yeahhh! (T-T)).

Comments

0

I have the same problem. What we are trying to do should not be a SOP problem because both pages are in the same computer. The problem is about the execution that eclipse do when we are testing the web. To solve the problem, copy the war folder into htdocs and run it using your internet explorer and you will check that your code is right. There is the possibility to config the run in eclipse, but I don't know how yet.

1 Comment

Please consider to put comments into comments instead of answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.