You can use jquery's $. ajaxsetup method to set the default parameter options for Ajax requests. WhenProgramWhen you need to initiate multiple Ajax requests, you do not need to configure the request parameters for each request.
$. Ajaxsetup method syntax
$. Ajaxsetup (properties) |
Parameters |
|
Properties |
(Object) an object instance. Its Attributes define a set of default Ajax attributes. These attributes are the same as the $. Ajax function attributes described above. |
Return Value |
Undefined |
Note that the default value set by the $. ajaxsetup function is not applied to the load () command. For utility functions such as $. Get () and $. Post (), the HTTP method is not overwritten by using these default values. Setting the default get type does not cause $. Post () to use the http get method.
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 () {// enabling the following line of code will cause Ajax request timeout // system. threading. thread. sleep (4000); 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 ;}
Run the program and Result
Download demo