Как получать и анализировать данные json, извлеченные с одной страницы jsp, на другую страницу jsp во время вызова ajax? - PullRequest
1 голос
/ 23 мая 2019

У меня есть простая страница jsp, на которой должны отображаться некоторые вопросы на английском языке и их идентификаторы, извлеченные из базы данных mysql, но без перезагрузки страницы.Для этой цели я использовал ajax на моей странице jsp demo.jsp, которая будет извлекать данные с другой страницы jsp fetch.jsp.Моя проблема в том, что я не получаю никаких данных в demo.jsp.Что мне делать?

demo.jsp

       <button onclick="fetch();">Fetch content</button>
       <br/>

       <table border="1px" width="500px;">
            <thead>
                <tr>
                    <th>Qid</th>
                    <th>Question</th>
                </tr>
            </thead>
            <tbody id="content"></tbody>
        </table>
    <script>
            function fetch() {
                console.log("test 1");
                $.ajax({

                       url:"fetch.jsp",
                       dataType:"json",
                       success:function(res){
                           console.log("test 2");
                          var data="";
                          for(i=0;i<res.length;i++){
                              var p=$.JSONparse(res[i]);
                              data+="<tr><td>"+p.qid+"</td><td>"+p.question+"</td></tr>";
                          }
                          $('#status').html("Status : Content fetched");
                          $('#content').html(data);
                       },
                       error:function() {
                           alert("error occured");
                       }
                   });
           }
        </script>

fetch.jsp

<% 
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/jigyasa";
String user="root";
String pass="";

    Connection con = null;
    Statement stmt = null;
    ResultSet result = null;
    response.setContentType("application/json");
    response.setHeader("cache-control", "no-cache");

    try{
        Class.forName(driver);
        con = DriverManager.getConnection(url,user,pass);
    }catch(ClassNotFoundException e){
    }catch(SQLException e){}

    try{

        String query;

        stmt = con.createStatement();

        query = "SELECT * FROM englishq";
        result = stmt.executeQuery(query);

        if(!result.next()){
            out.print("0");
        }else{
            JSONArray array=new JSONArray();
            do{
                JSONObject obj = new JSONObject();
                obj.put("qid",result.getString("qid"));
                obj.put("question",result.getString("question"));
                System.out.println(result.getString("qid"));  
                System.out.println(result.getString("question")); 
                array.put(obj);

            }while(result.next());
             out.print(array.toString());

            out.flush();
         }

    }catch(SQLException e){
        out.print("Exception: "+e);
    } 

%>




Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...