2012-12-06 2 views
1

간단한 HTML에서 JSON을 얻으려고하고 있지만 성공적으로 할 수 없다. C .. 나는 java 서블릿에서 JSON을 생성한다. 내가 다른 페이지를 내 자바 프로젝트에서서블릿 (java) + mysql에서 간단한 HTML은 getJSON을 할 수 없다.

{"grifo":"Grifo Libertad","distrito":"San Juan de Lurigancho", 
"latitud":"-123.059028347924","longitud":"93.945783454234234"} 

:이 프로그램을 실행할 때이 ...

Prueba.java (서블릿)

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을 얻을 index.jsp, JSON을 가져옵니다.

나는 제대로 JSON을 얻을,하지만 난 .html 중에서 만들 때 (바탕 화면에서 : 파일 : /// C : /Users/Jhonatan/Desktop/prueba.html 웹 브라우저에서) 같은 코드를 : http://freetexthost.com/w2xgabhaks

JSON을 얻을 수 없으며 아무 것도 표시하지 않습니다. 서버 (.jsp), 다른 위치 (desktop : .html) 또는 데이터베이스 (mysql)의 파일에 문제가 있습니까?

https://graph.facebook.com/zombies은 어떻게 작동하나요?

여러분 모두 감사드립니다!

+0

JSP에서 사용할 수있는 jSON 개체는 무엇입니까? –

+0

만약 내가 이것을 얻으면? http://img208.imageshack.us/img208/1864/json.jpg – JhonatanSandoval

+1

html 파일도 서버 위치에두고 http : // localhost : 8080/whatever/filename.html로 액세스하고 데스크탑이 아닌 if를보십시오. 그것은 작동합니다. 데스크톱에 있다면 브라우저가 동적 요청을 수행하기 위해 어떤 경로를 추측한다고 생각합니다. – Subin

답변

0

JS 콘솔의 내용을 보았습니까? 내 생각 엔 Origin null is not allowed by Access-Control-Allow-Origin 같은 것을 던질 것입니다.

자바 스크립트 요청은 동일 원산지 정책을 사용합니다. 당신이하려는 것은 cross-origin request입니다.

간단한 수정 서버는 다음과 같은 헤더를 반환 할 수 있습니다 :

Access-Control-Allow-Origin: * 

을하지만 난 당신이 all about this를 읽고 의미가 무엇인지 이해하는 것이 좋습니다.

관련 문제