A common effect, when using AJAX requests, is not to return before the occurrence of a rotating loading small icon or "content loading ..." to inform the user is requesting data. This can be achieved using the Beforesend method.
Download Demo:ajax loading
The code is as follows:
Copy Code code as follows:
function Test_ajax () {
$.ajax (
{
Type: "Get",//usually uses two kinds: get,post. The default is: Get
URL: "a.php",//(Default: Current page address) send the requested address
DataType: "HTML",//expected data type returned by the server.
Beforesend:beforesend,//Send Request
Success:callback,//Request success
error:error,//Request Error
complete:complete//Request Complete
});
}
Function error (XMLHttpRequest, Textstatus, Errorthrown) {
Typically, only one of the Textstatus and Errorthown has a value.
$ ("#showResult"). Append ("<div> request is wrong!") </div> ");
}
function Beforesend (XMLHttpRequest) {
$ ("#showResult"). Append ("<div><div>");
}
function complete (XMLHttpRequest, textstatus) {
$ ("#showResult"). Remove ();
}
function Callback (msg) {
$ ("#showResult"). Append ("Successful <div> request, return number:" +msg+ "<div>");
}
Method Beforesend, which is used to add some handler functions before sending a request to the server. This is an AJAX event that is triggered before the AJAX request starts, usually allowing the user to modify the XMLHttpRequest object (for example, setting additional header information), and an explanation of the Ajax event can be referenced in the documentation: Http://docs.jquery.com/Ajax_Events
We also see a situation, many Web sites in the process of loading content in the "Data loading, please wait," the prompt, when the content is loaded after the display content. You can set the default text to appear as a prompt in load, and when the content is loaded, we can replace the text in the label with the final content by ID selector. In order to replace the beforesend, the efficiency is higher.
When to use Beforesend, and when to replace it with text, depends on whether the DOM elements you show are consistent before and after the AJAX request, and if the DOM element you are presenting is already there before the request, then it would be better to do so by replacing the text with the above, if you need to add additional requirements. , then use Beforesend to deal with it.