jquery - Pass JSON String using Ajax in Java EE -


mongodb doesn't have built-in restful interface trying convert mongodb query result string format , send on using ajax giving me error

servletdemo.java : com.mongodb.servlets

public void dopost(...){     returnstring(); } public string returnstring(){     mongoclient mongoclient = new mongoclient(new mongoclienturi("mongodb://localhost:27017"));     db database = mongoclient.getdb("db");     dbcollection collection = database.getcollection("coll");     dbobject getdocs = new basicdbobject();     dbcursor cursor = collection.find(getdocs);     while(cursor.hasnext()){         returnstring += string.format("%s",cursor.next());     }     return returnstring; } 

index.html

<body>     <button>click me</button>     <p></p>     <script>         $(document).ready(function(){             $("button").on("click",function(){                 $.ajax({                     url:'servletdemo',                     type:'post',                     success:function(data) {                         alert("success");                         $('p').html(data);                     },                     error:function(msg){                         alert("error");                         console.log(msg);                     }                 });             });         });     </script> </body> 

changes in servletdemo.java

pass data using response.getwriter() ajax call

public void dopost(...){     mongoclient mongoclient = new mongoclient(new mongoclienturi("mongodb://localhost:27017"));     db database = mongoclient.getdb("db");     dbcollection collection = database.getcollection("coll");     dbobject getdocs = new basicdbobject();     dbcursor cursor = collection.find(getdocs);     while(cursor.hasnext()){         returnstring += string.format("%s",cursor.next());     }     response.getwriter().write(returnstring); } 

working now...


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -