This example summarizes the common ways in which jquery invokes Ajax requests. Share to everyone for your reference. Specifically as follows:
Example code 1
$.ajax ('/route ', {
type: ' Get '
data: {param1: ' Hello ', param2: ' The World '},
dataType: ' JSON ',
ContentType: ' Application/json ',
timeout:3000,
success:function (response) {
//Console.log ( response.something);
},
error:function (Request, ErrorType, errormessage) {
//Console.log ("[" + ErrorType + "]" + errormessage);
},
beforesend:function () {
//do something like. addclass (' is-fetching ' )
},
complete:function () {
//do something like Removeclass (' is-fetching ')
}}
);
Example code 2
$.get ('/route ', function (response) {
//success (response:html)
});
$.getjson ('/route ', function (response) {
//success (Response:json)
});
Example code 3
$ (' form '). On (' Submit ', function (event) {
event.preventdefault ();
var FormData = $ (this). Serialize ();
$.ajax ($ (this). attr (' action '), {
type: $ (this). attr (' method '),
data:formdata,
dataType: ' JSON ',
ContentType: ' Application/json ',
success:function (response) {},
error:function (Request, ErrorType, errormessage) {},
beforesend:function () {},
complete:function () {},
timeout:3000
})
;
I hope this article will help you with your jquery programming.