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:
- What’s the proper way of appending query string parameters into the
urlforGETs? - What’s the proper way of adding form data into
POSTs? - I see that the
successfunction takes three params:data,textStatus, andjqXHR, but many examples I see only list thedataportion – when do you pass it justdataand when do you pass it all three? - What’s the difference between the
get‘sdataproperty and itssuccess: function (data) { ... }argument? - 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.
jQuery Get/Post Params
FYI, I believe the url param is the only required param. Your get call should look something along these lines:
By the way, you can also serialize a form to pass it through your post using something like:
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: