28 June 2015

Ajax call in MVC

Ajax call in MVC

The first thing to look at is the key settings options that are available for AJAX requests:

type This is type of HTTP Request and accepts a valid HTTP verb. POST is the option illustrated in this article.
url This is the location of the resource that the request will be made to.
data This is the actual data to be sent as part of the request.
contentType This is the content type of the request you are making. The default is 'application/x-www-form-urlencoded'.
dataType This is the type of data you expect to receive back. Accepted values are text, xml, json, script, html jsonp. If you do not provide a value, jQuery will examine the MIME type of the response and base its decision on that.

Example

< script >
   $( function () {
        $( "button" ).click( function () {
            var car = { Make: 'Audi', Model: 'A4 Avant', Colour: 'Black', Registered: 2013 };
            $.ajax( {
                type: "POST",
                url: "/Receiver",
                data: car,
                datatype: "html",
                success: function ( data ) {
                    $( '#result' ).html( data );
                }
            } );
        });
    } );

< /script >


No comments:

Post a Comment

Comments Welcome