Introduction to the use of the Beforesend method of Jquery.ajax _jquery

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.