12.10. AJAX Methods

It is very common for performance optimization to need to retrieve data or code from an external source after a page is loaded. jQuery makes this very easy.

12.10.1. Load and Run JavaScript File

The following code loads an external JavaScript file called test.js and then executes it:

$.ajax({
    type: "GET",
    url: "test.js",
    dataType: "script"
});

The test.js file consists of just the following line:

alert('hello');

12.10.2. Submitting Data

You often need to submit data to another web page. You can easily do this with the following code:

$.ajax({
    type: "POST",
    url: "myForm.aspx",
    data: "firstname=Alex&lastname=Mackey",
    success:
    function() {
        alert("form submitted");
    }
});

You will use this functionality in the ASP.NET MVC example ...

Get Introducing .NET 4.0: with Visual Studio 2010 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.