dry69034 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() {
    @Override
    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().clear();
                                  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);
                        }

    }

    @Override
    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
e.printStackTrace();
}

my php file:

     <?php

  header('Content-Type: text/javascript');
  header('Cache-Control: no-cache');
  header('Pragma: no-cache');
  $str = $_POST['param'];
  $con=mysqli_connect("llxxllxxx","lll","lll","lll");
  // 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; 
             $i++; 
         } 
         $result->close(); 
         $con->close(); 

      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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集