Jquery Ajax request (1)

Source: Internet
Author: User
Jquery. Ajax (options) Request

Http://s.click.taobao.com/t_8? E = 7hz5x % 2bozdswsvvyc5jts79au1q % 3d % 3d & P = mm_24156262_0_0

Jquery underlying Ajax implementation. For easy-to-use high-level implementation, see $. Get, $. Post, and so on.

$. Ajax () returns the created XMLHTTPRequest object. In most cases, you do not need to directly operate on this object, but in special cases it can be used to manually terminate the request.

$. Ajax () has only one parameter: The parameter key/value object, which contains information about various configurations and callback functions. For detailed Parameter options, see.

Returned value: XMLHttpRequest

Parameter quantity: Optional

Optional items:Async(Boolean): (default: True) All requests are asynchronous requests by default. To send a synchronization request, set this option to false.

Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.

Beforesend(Function): Before sending a request, you can modify the function of the XMLHTTPRequest object, for example, adding a custom HTTP header. The XMLHTTPRequest object is a unique parameter. Ajax events.

Cache(Boolean): (default: True, false by default when datatype is set to script). Setting it to false will not load request information from the browser cache.

Complete(Function): callback function after the request is complete (information about successful or failed requests ).

Contenttype(String): (default: "application/X-WWW-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications.

Data(Object, string): the data sent to the server.

Datafilter(Function): A function used to pre-process the original data returned by Ajax.

Datatype(String): The expected data type returned by the server.

"XML": returns an XML document, which can be processed by jquery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavascriptCode. Results are not automatically cached. Unless the "cache" parameter is set

"JSON": Return JSON data.

"Jsonp": jsonp format. When calling a function in the form of jsonp, such as "myurl? Callback =? "Will jquery be replaced automatically? For the correct function name to execute the callback function.

"Text": returns a plain text string.

Jquery code:

Load and execute a JS file.

$. Ajax ({
Type: "Get ",
URL: "test. js ",
Datatype: "script"
});
Save the data to the server. If the data is successfully saved, the information is displayed. $. Ajax ({
Type: "Post ",
URL: "Some. php ",
Data: "name = John & location = Boston ",
Success: function (MSG ){
Alert ("data saved:" + MSG );
}
});

Load the latest HTML webpage version

$. Ajax ({
URL: "test.html ",
Cache: false,
Success: function (HTML ){
$ ("# Results"). append (HTML );
}
});

Load data synchronously. The browser is locked when a request is sent. You must use the synchronization method to lock user interaction operations.

VaR html = $. Ajax ({
URL: "Some. php ",
Async: false
}). Responsetext;

Event column:

Jquery code:

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
$ (). Ready (function (){
$. Ajax ({type: "Post", URL: "jqueryadddata. ashx", success: function (MSG ){
$ ("# Divshow" cmd.html (MSG );
}
});
$. Ajax ({type: "Post", URL: "jqueryadddata. ashx", data: "name = John & age = 32", success: function (MSG ){
Alert (MSG );
}
});


});
</SCRIPT>

 

Jqueryadddata. ashx (CODE)

<% @ Webhandler Language = "C #" class = "jqueryadddata" %>

Using system;
Using system. Web;
Using system. Data;
Using system. Data. sqlclient;
Using system. text;

Public class jqueryadddata: ihttphandler
{

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
// Context. response. Write ("Hello World ");
Context. response. Write (selectadd ());
String name = httpcontext. Current. Request. Params ["name"]. tostring ();
String age = httpcontext. Current. Request. Params ["Age"]. tostring ();
Context. response. Write (insertdatatable (name, age ));
}

Public bool isreusable
{
Get
{
Return false;
}
}

Public String selectadd ()
{
String SQL = "select fileid, [filename] From sysfiles ";
String connstring = @ "Data Source = VANSKY-34F1 \ sqlexpress; initial catalog = Master; Integrated Security = true ";
Stringbuilder builder = new stringbuilder ();
Builder. append ("<Table> ");
Builder. append ("<tr> <TD> file name </TD> <TD> path </TD> </tr> ");
Using (sqlconnection connection = new sqlconnection (connstring ))
{
Connection. open ();
Sqlcommand command = new sqlcommand (SQL, connection );
Sqldatareader reader = command. executereader (commandbehavior. closeconnection );
While (reader. Read ())
{
Builder. append ("<tr> <TD>" + reader [1]. tostring () + "</TD> </tr> ");
}
Connection. Close ();
Builder. append ("</table> ");
Return builder. tostring ();
}

}

Public String insertdatatable (string name, string age)
{
String SQL = "insert into datatable values (@ name, @ age )";
String connstring = @ "Data Source = VANSKY-34F1 \ sqlexpress; initial catalog = dbtest; Integrated Security = true ";
Using (sqlconnection connection = new sqlconnection (connstring ))
{
Connection. open ();
Sqlcommand command = new sqlcommand (SQL, connection );
Command. Parameters. addwithvalue ("@ name", name );
Command. Parameters. addwithvalue ("@ age", age );
Int reader = command. executenonquery ();
If (Reader> 0)
{
Return "successful! ";
}
Else
{
Return "failed! ";
}
}

}

} Http://s.click.taobao.com/t_8? E = 7hz5x % 2bozdswsvvyc5jts79au1q % 3d % 3d & P = mm_24156262_0_0

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.