How do i get json data using jquery ajax on jsp page.Please can anyone suggest me some function for it.I have to pass data coming from servlet to javascript like var data={"some json data"}
3 Answers
Seems like the jQuery docs for jQuery.getJSON() has examples.
2 Comments
Siddharth
Can u Suggest one of the examples here
Mike Mooney
The link he provided does provide an example.
1) Add JQuery into your page: http://jquery.com/
2) Use the getJSON method to call back to the server-side JSP page to retrieve the JSON data: http://api.jquery.com/jQuery.getJSON/
3) If you want to parse the JSON data into an object, use Douglas Crockford's JSON library: https://github.com/douglascrockford/JSON-js
Comments
Yes, you should retrieve from servlet the json string, and read it using a function like getJSON
$.getJSON('ajax/json_data.php', function(data) {
// do whatever you need with data
});
7 Comments
Siddharth
can i specify url as my servlet file path or as localhost:8080/proj/action
Siddharth
My error console is giving error "$ is not defined" what might me the problem
yoda
You should first check how jquery works, otherwise you will not be able to understand it : jquery.com
Siddharth
$(document).ready(function() { //Retrieve the JSON data from the server using AJAX $('#AJAXButton').click(function() { $.getJSON( "/tpm/src/com/gr/view/Five2hRptServlet.java?action=FiveW2HRpt_input.fwh", function(data) { alert("data="+data);}); }); }); This is the function i am using but its not working i am calling by <input id="AJAXButton" type="button" value="Get JSON With AJAX" /> Where do u think its going wrong?
yoda
Are you using a file path or a url?
|