jQuery provides a
.ajax() method that provides a way to make ajax requests that works across all browsers. Typically, you will have an event listener (e.g., for clicking a submit button), and in the callback for that event, you will call
$.ajax() to make the ajax request in response.
.ajax() method takes an object with various paramters, many of which are optional. Here are the most important ones:
- url - The url route that the request will go to.
- type - The type of HTTP request (e.g. "GET", "POST").
- data - The params to provide to the server.
- success - The callback function to be run upon successful completion of the controller action.
- error - The callback function to be run upon an unsuccessful request.
You will want to make sure that the controller is responding to you with JSON rather than the default HTML. There are various ways to specify this in different places in your application, but if you haven't set it up elsewhere, you can include the dataType parameter with the value