0

I have a jQuery web app frontend that I would like to make GET/POST ajax calls to a Java backend that is running on Spring MVC.

Here is the GET request I want to make:

http://www.myapp.com/backend/doSomething?a=1&b=2

On the server-side, here is my BackendController object:

@RequestMapping(value = "/backend/doSomething", method = RequestMethod.GET)
public ModelAndView handleDoSomething(@RequestParam("a") String a,
    @RequestParam("b") String b) {

    ModelAndView mav = new ModelAndView();
    mav.setViewName("backend/SomeView");

    // process the request...

    return mav;
}

So here's the jQuery I have attempted so far:

$.get({
    url: "/backend/doSomething?a=???&b=???",
    success: function(data) {
    }
    ???
});

I've read the jQuery $.get page and I'm still confused about several things:

  1. What's the proper way of appending query string parameters into the url for GETs?
  2. What's the proper way of adding form data into POSTs?
  3. I see that the success function takes three params: data, textStatus, and jqXHR, but many examples I see only list the data portion - when do you pass it just data and when do you pass it all three?
  4. What's the difference between the get's data property and its success: function (data) { ... } argument?
  5. Is there any special configuration I need to do in my Spring backend so that jQuery can connect to it, or does jQuery not care about the backend at all?

I've tried looking these up but can't seem to get clear definitions for these items. Thanks in advance.

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

3

jQuery Get/Post Params

  1. Url
  2. Data - the data object which is where you would store the query string like data (ie post variables)
  3. Success Handler (the extra params are not required which would be why you have seen it in different formats)
  4. DataType - the data type expected back from the server

FYI, I believe the url param is the only required param. Your get call should look something along these lines:

$.get("/backend/doSomething", { a : a, b : b }, success: function(data) {
    // Perform Success code
});

By the way, you can also serialize a form to pass it through your post using something like:

$('form').serialize()

Edit:

Regarding naming the url, data, and success params when using jQuery $.get() and $.post(), this is not possible. These functions are shorthand versions of jQuery $.ajax(). There is no point in using get/post if you are going to what to do the long form anyways. If you want to specify the params then use ajax like so:

$.ajax({
    url: "/backend/doSomething", 
    data: { a : a, b : b }, 
    success: function(data) {
        // Perform Success code
    }
});
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • Thanks @Josh (+1) - quick followup: then what `data` is going into the success function, and who passes it in? Is it the results from the backend? If not, how do I access the results of the backend? Thanks again! – IAmYourFaja Sep 18 '12 at 16:49
  • It is the results from the backend. – Josh Mein Sep 18 '12 at 16:50
  • Thanks again @Josh Mein; last question: in your example, would it be valid JavaScript to have qualified the first 2 params with their name, such as: `$.get(url: "/backend/doSomething", data: { a: 1, b: 2}, ...` etc.? Thanks again! – IAmYourFaja Sep 18 '12 at 19:18
  • @4herpsand7derpsago In JavaScript you cannot quialify / name function arguments, so `$.get(url: "/backend/doSomething", data: { a: 1, b: 2}, ...` **won't work**. Oth the other hand since `$.get` is only shortcut for `$.ajax`, you can use the second one's different definition (it accepts an Object) and do `$.ajax({ url: '/doSth', data: { a: 1, b: 2 }, success: function(res) { /* success */ } });` – Grzegorz Rożniecki Sep 19 '12 at 21:48
  • @4herpsand7derpsago I have updated my answer regarding your last question. – Josh Mein Sep 20 '12 at 12:17
1

It's all there... (I mean in jQuery API and in the Internet.)

Basically you use $.get like this:

$.get('/backend/doSomething', // 1st argument: URL
  { a: 1, b: 2 }, // 2nd arg (optional): JS Object or String you want to pass
  function(data) { // 3rd arg (or 2nd in case data is missing) success callback
    alert('Success!'); // or sth else
});

So back to your questions:

  1. Just pass second argument to $.get.
  2. Use $.post instead (they're both similar and are just wrappers to $.ajax, see docs).
  3. Usually you'll use only first parameter - data, in JavaScript method signatures doesn't have to match (speaking Java-like). You can ignore parameters if you like:
  4. data as 2nd argument of $.get are input params you give to your controller, and success: function (data) is response output (probably @ResponseBody JSONified-by-Jackson object). I mean:

    function(data) {
      alert('Success! Data ' + data);
    }
    

    and

    function(data, textStatus, jqXHR) {
      alert('Success!');
    }
    

    are equivalent.

  5. No, jQuery is just JavaScript library which makes things easier. Just use them both like there's written in docs.

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
  • Thanks @Xaerxess (+1) - two follow ups: (1) please see my last question underneath Josh Mein's answer - I have the same question for you! And (2) I see you `function` examples above aren't named (`function(data)` instead of `function doSomething(data)`, etc). What are these name-less functions and how are they called (if they don't have a name)? Thanks again! – IAmYourFaja Sep 18 '12 at 19:20
  • @4herpsand7derpsago You don't need to call them. They will automatically be called when the jquery get/post call completes. They are known as callback functions. – Josh Mein Sep 18 '12 at 19:25
  • Indeed, 'callback' is the key word here. See [related question](http://stackoverflow.com/questions/824234/what-is-a-callback-function). – Grzegorz Rożniecki Sep 18 '12 at 19:37