Use of global functions for Ajax requests

Source: Internet
Author: User

We can append the Ajax global function to a specific Dom element. These functions will be triggered at different stages of Ajax request processing or when the request is ultimately successful or fails.

Any command syntax of the Ajax global function is consistent, so it is described in the following table.

Ajaxstart (callback)

Ajaxsend (callback)

Ajaxsuccess (callback)

Ajaxerror (callback)

Ajaxcomplete (callback)

Ajaxstop (callback)

Attaches the passed callback function to all matching elements. Once the Ajax request is processed, the callback function is triggered.

Parameters

 

Callback

(Function) will be appended with the callback function. Refer to the following table to learn when the callback function is triggered and what parameters will be passed.

Return Value

Package set

Ajax global callback function (in the order of triggering)

Global Function Type

When is triggered

Parameters

Ajaxstart

When a jquery Ajax function or command is initiated, but before the xhr instance is created

Global callback information object whose type is set to ajaxstart

Ajaxsend

After the xhr instance is created, but before the xhr instance is sent to the server

Type is set to the global callback information object of ajaxsend; xhr instance; $. Ajax () function attribute

Ajaxsuccess

After the request has been returned from the server, and the response contains the success status code

Type is set to the global callback information object of ajaxsuccess; xhr instance; $. Ajax () function attribute

Ajaxerror

After the request has been returned from the server, the response contains a failure status code.

Global callback information object whose type is set to ajaxerror; xhr instance; $. Ajax () function attribute; Exception object returned by xhr instance (if any)

Ajaxcomplete

After the request has been returned from the server and any well-known ajaxsuccess or ajaxerror callback function has been called

Global callback information object whose type is set to ajaxcomplete; xhr instance; attributes used by the $. Ajax () function

Ajaxstop

After all other Ajax processing and any other applicable global callback functions have been called

Global callback information object whose type is set to ajaxstop

Let's look at an example.

ClientCode:

<HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Main server code:

 
Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {If (request ["ID"]! = NULL &&! String. isnullorempty (request ["ID"]. tostring () {response. write (getdata (request ["ID"]. tostring () ;}} protected string getdata (string ID) {string STR = string. empty; Switch (ID) {Case "1": Str + = "this is number 1"; break; Case "2": Str + = "this is number 2 "; break; Case "3": Str + = "this is number 3"; break; default: Str + = "warning other number! "; Break;} return STR ;}

RunProgram, Result

We also confirmed the execution sequence of global functions.

Download demo

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.