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条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus