$.ajax({
type: "GET",
url: "Services/EFService.svc/Members",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with returned data in JSON format
},
error: function (msg) {
alert(msg);
}
});
Global events are triggered by every Ajax request. They are broadcast to all elements in the DOM. The script loaded in the getScript() example above is the global Ajax event. . Global events can be defined as follows:
$("#loading" ).ajaxStart(function(){
$(this).show();
});
We can disable global events in specific requests, just set global The options are fine:
$.ajax({
url: "test.html",
global: false,// Disable global Ajax events. // ... });
The following is the complete Ajax officially given by jQuery Event list:
•ajaxStart (Global Event)
This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.
•beforeSend (Local Event)
This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)
•ajaxSend (Global Event)
This global event is also triggered before the request is run.
•success (Local Event)
This event is only called if the request was successful (no errors from the server, no errors with the data).
•ajaxSuccess (Global Event)
This event is also only called if the request was successful.
•error (Local Event)
This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request) .
•ajaxError (Global Event)
This global event behaves the same as the local error event.
•complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
•ajaxComplete (Global Event)
This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.
•ajaxStop (Global Event)
This global event is triggered if there are no more Ajax requests being processed.
jQuery.ajaxSetup( options ) : Set global AJAX default options.
Set the default address of AJAX requests to "/xmlhttp/", disable triggering of global AJAX events, and use POST instead of the default GET method. Subsequent AJAX requests will not set any option parameters.
jQuery code:
$.ajaxSetup({
url: "/xmlhttp/",
global: false,
type: "POST"
});
$.ajax({ data: myData });
If specified as html type, any embedded JavaScript will be executed before the HTML is returned as a string. Similarly, if the script type is specified, the server-side generated JavaScript will be executed first, and then the script will be returned as text data.
JSON data is a kind of structured data that can be easily parsed through JavaScript. If the obtained data file is stored on a remote server (with different domain names, that is, cross-domain data acquisition), you need to use the jsonp type. Using this type creates a query string parameter callback=? which is appended to the requested URL. The server should add the callback function name before the JSON data in order to complete a valid JSONP request. If you want to specify the parameter name of the callback function instead of the default callback, you can set the jsonp parameter of $.ajax().