2014-04-24 18:57
浏览 63

GWT PHP从Webserver获取数据

I have a Webpage with a MYSQL Database. I can easily Insert data in it but when it comes to "Select" it throws "("Couldn't retrieve JSON")" is it even possbile to get this data from a Webserver with the jetty Server. I acually plan to use Phonegap which supports PHP http://phonegap.com/2012/05/02/phonegap-explained-visually/

       RequestBuilder request = new RequestBuilder(RequestBuilder.POST,URL.encode("http://testeddd.com/test.php"));

   String name = mioUsername.getText();
String pssw = miaPassword.getText();
        JSONObject jsonValue = new JSONObject();
        jsonValue.put("myusername", new JSONString(name));
        jsonValue.put("mypassword", new JSONString(pssw));
        request.setHeader("Content-Type", "application/json");
        try {
request.sendRequest(jsonValue.toString(),new RequestCallback() {
    public void onResponseReceived(Request request, Response response) {
 if (200 == response.getStatusCode()) {
                                      try {
                            JSONValue jsonValue = JSONParser.parse(response.getText());
                            JSONArray jsonArray = jsonValue.isArray();
                            if (jsonArray != null) {

                                  RootPanel.get().add(new MapComp());

                        } else {
                             Dialogs.alert("1", "fault", null);

                        }     } catch (JSONException e) {
                             Dialogs.alert("2",  e.toString(), null);
                        } else {
                             Dialogs.alert("request none",  "Couldn't retrieve JSON (" + response.getStatusText() + ")", null);


    public void onError(Request request, Throwable exception) {
        //displayError("Couldn't retrieve JSON");
} catch (com.google.gwt.http.client.RequestException e) {
// TODO Auto-generated catch block

my php file:


  header('Content-Type: text/javascript');
  header('Cache-Control: no-cache');
  header('Pragma: no-cache');
  $str = $_POST['param'];
  // Check connection
if (mysqli_connect_errno())
  echo "Failed to connect to MySQL: " . mysqli_connect_error();

$result = mysqli_query($con,"SELECT * FROM xxx".$str);

   $i = 0; 
         while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) 
             $array[$i] = $row; 

      print json_encode($array); 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doqp87012 2014-04-24 20:16

    It is a problem with the same origin policy or the domain whitelist.

    If you load a page from one domain (or same domain and different port / protocol) the browser will send the request, but don't deliver the answer too your javascript.

    It will be the statuscode 0 and an empty message.

    To send CORS-Header may also help.

    打赏 评论
  • doubiaode0460 2014-04-24 19:26

    Don't think you should URL.encode the entire URL.

    URL encoding the entire URL will result in http://testeddd.com/test.php becoming http%3A%2F%2Ftesteddd.com%2Ftest.php

    打赏 评论

相关推荐 更多相似问题