jQuery ajax

If you want to dynamically load content into your page chances are your going to need to do some sort of request to a server in the background. The simplest way of doing this is to use jQuery’s ajax function.

Here’s an example of a GET request and a POST request:

        $.ajax({
            type: "POST", // Set the HTTP type to POST
            url: "/serviceurl.aspx", // URL of the service
            data: "{data: [{"Field1": 1}] }", // Data to be posted to the service
            processData: false, // If set to true the data will be processed and turned into a query string
            success: (function (result) {
                // Result of the service call
            }),
            error: (function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            })
        });

        $.ajax({
            type: "GET", // Set the HTTP type to GET
            url: "/serviceurl.aspx", // URL of the service
            dataType: "jsonp", // Set what type of data you want back
            success: (function (result) {
                // Function that will be called on success
            })
        });
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s