Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json parser empty result

Tags:

json

php

gwt

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);
like image 571
user315196 Avatar asked May 05 '26 02:05

user315196


1 Answers

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.

like image 95
Just4Test Avatar answered May 07 '26 18:05

Just4Test



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!