4

Since getJSON() method is used to get JSON data using an AJAX HTTP GET request. Which method to use to get data from doPost method in servlet. Say that I have used action to send the data to the servlet and want to get the response .. Which JSON method to use..An example or a good tutorial would help

Thanks:)

Gamlor
  • 12,978
  • 7
  • 43
  • 70

2 Answers2

0

You can do this:

$.post(url, function(), return_type); //where return_type you replace with 'json'

So essentially getJSON() is just an alias for:

$.get(url, function(), 'json'); 

Here is a little plugin i made for postJSON:

(function($){
 $.postJSON = function(url, data, ret_fn) {
    return $.post(url, data, ret_fn, 'json');
 };
})(jQuery);

And here is fiddle: http://jsfiddle.net/maniator/H8YeE/

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • @Neal- can you quote me an example...I am a begineer..It would be more clear..Also any good tutorials would help since the ones I saw were simple..Thanks:) –  May 09 '11 at 18:34
  • What do you mean, by `example`? i think i was pretty clear :-P – Naftali May 09 '11 at 18:35
  • @Neal-Well $.post is sending the parameters to the servlet rite...What I meant was say I have sent the parameters through
    ,then how do I retreive data from the server using JSON???why is'nt there a method like getJSON() for post??any particular reason??why not some thing like postJSON like getJSON()??
    –  May 09 '11 at 18:41
  • i just told u. do `$.post(url, function, 'json')`, that is `$.postJSON`, for some reason jQuery never made a `postJSON` method, but that is the same thing – Naftali May 09 '11 at 18:42
  • @Neal...Made it neat and simple:) –  May 09 '11 at 18:50
  • @Neal-could understand it well..All doubts got cleared...tat s what I meant!!:) –  May 09 '11 at 18:54
  • ahhhh haaaa. but yet, you chose the more complicated answer :-p – Naftali May 09 '11 at 18:54
  • I saw the answer before you editing...I have voted for yours:D I understood both answers..there should be an option to accept more than one answer...:P –  May 09 '11 at 18:56
0

So, you're basically asking how to get JSON data by a POST request?

Instead of

$.getJSON('servleturl', function(data) {
    alert(data);
});

use

$.post('servleturl', function(data) {
    alert(data);
});

When you let the servlet do response.setContentType("application/json"), then the data is already in JSON format.


However, after reading your question and the comments once more, I think you're basically asking how to submit a POST form using jQuery. This has essentially nothing to do with JSON (although the servlet can return a JSON response if desired).

Assuming the following form

<form id="formid" action="servleturl" method="post">
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <input type="submit" />
</form>

here's how you can "ajaxify" it (do it during document ready!)

$('#formid').submit(function() {
    $form = $(this);
    $.post($form.attr('action'), $form.serialize(), function(data) {
        // Do something with response. Display message? Redirect to other page?
        alert(data);
    });
});

For another example also see this answer.

There exist plugins which do it more nicely, such as jQuery Form. It's then as simple as

$('#formid').ajaxForm(function(data) {
    // Do something with response. Display message? Redirect to other page?
    alert(data);
});

and it also supports <input type="file"> elements without much trouble.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC-Yes exactly...BalusC you seem to know lot about it..can u suggest me some good tutorials..little complex ones..Thanks:) –  May 09 '11 at 18:46
  • For JSP/Servlets, check the links at the bottom of our wiki pages: [JSP](http://stackoverflow.com/tags/jsp/info) and [Servlets](http://stackoverflow.com/tags/servlets/info). For jQuery, check the tutorials mentioned at [its homepage](http://jquery.com) (and I recommend the book "jQuery in Action"). For JSP/Servlet/jQuery combo, check those answers: [update current page with servlet](http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet) and [simple calculator in JSP](http://stackoverflow.com/questions/4114742/simple-calculator-in-jsp). – BalusC May 09 '11 at 18:51