我試圖從一個簡單的HTML獲取JSON,但是我無法成功完成:c ..我從一個Java Servlet生成JSON,從MySQL生成這...簡單的HTML無法從servlet(java)獲取json + mysql
Prueba.java(Servlet的)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Connection conn = ConexionBD.obtenerConexion();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM grifo where id=1");
while(rs.next()){
String grifo = rs.getString("grifo");
String distrito = rs.getString("distrito");
String latitud = rs.getString("latitud");
String longitud = rs.getString("longitud");
JSONObject json = new JSONObject();
json.put("grifo", grifo);
json.put("distrito", distrito);
json.put("latitud", latitud);
json.put("longitud", longitud);
out.print(json);
}
} catch (Exception e){
out.print(e);
}
}
所以,當我運行它,我得到的JSON:
{"grifo":"Grifo Libertad","distrito":"San Juan de Lurigancho",
"latitud":"-123.059028347924","longitud":"93.945783454234234"}
在我的Java項目中,我有另一頁稱爲index.jsp,即獲取JSON。
我正確地得到JSON,但是當我創建一個的.html(在桌面上:文件:/// C:/Users/Jhonatan/Desktop/prueba.html在web瀏覽器)使用相同的代碼: http://freetexthost.com/w2xgabhaks
我無法得到JSON,只顯示任何東西!服務器上的文件(.jsp),其他位置(desktop:.html)還是數據庫(mysql)有問題?
https://graph.facebook.com/zombies如何工作呢?
謝謝大家!
是您的JSP可用的jSON對象嗎? –
你的意思是,如果我得到這個? http://img208.imageshack.us/img208/1864/json.jpg – JhonatanSandoval
嘗試把你的html文件也放在服務器位置,並以http:// localhost:8080/whatever/filename.html的形式訪問它,而不是桌面,並查看是否有用。如果它在桌面上,我認爲瀏覽器會進行一些路徑猜測以使動態請求起作用。 – Subin