Chapter 25B. Using jQuery for Ajax in MVC

In Lesson 23 you learned about using JavaScript to perform client-side programming. In that lesson you were introduced to the jQuery script library. In Lesson 24B you learned how to use the Ajax helpers provided by the ASP.NET MVC framework. In this lesson I teach you how to use the Ajax tools provided by jQuery.

AJAX METHODS

These are the Ajax methods that jQuery provides:

  • $.ajax — This method performs an Ajax request. All of the other methods in this list are shortcuts that use this method.

  • $.get — This method loads HTML using an HTTP GET request.

  • $.getJSON — This method loads JSON-encoded data using an HTTP GET request.

  • $.getScript — This method loads a JavaScript file using an HTTP GET request and executes it.

  • .load — This method loads HTML into the specified element.

  • $.post — This method loads HTML using an HTTP POST request.

    Note

    JSON stands for JavaScript Object Notation. It is a format used to represent simple data structures and objects in a lightweight, text-based, human-readable format.

The simplest way to fetch data asynchronously from the server is to use the .load method. These are the parameters of the .load method:

  • url — This is a string that contains the URL to which the request is sent. This is the only required parameter.

  • data — This is a string that contains any data that needs to be sent to the URL.

  • complete — This is the function that is called when the request completes.

The .load method uses a GET method to fetch data from the server, ...

Get ASP.NET 4 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.