Asp. Net + Jquery. Ajax details 3-$. get and $. post

Source: Internet
Author: User

JQuery. get (url, [data], [callback], [type])

Load information through remote http get requests

 

Parameter --

Url: the requested url.

Data: Key/value parameter to be sent.

Callback: callback function when the load is successful.

Type: The returned content format is xml, html, script, json, text, and _ default.

 

The get () method provides the callback function, which has three parameters: responseText, textStatus, and XMLHttpRequest, which respectively represent the content, Request status, and XMLHttpRequest objects returned by the request.

 

[Javascript]
$. Get ("Data/GetServiceInfo. aspx", function (responseText, textStatus, XMLHttpRequest ){
 
// ResponseText: Content returned by the request
 
// TextStatus: Request status: success, error, notmodified, timeout
 
// XMLHttpRequest: XMLHttpRequest object
 
});

$. Get ("Data/GetServiceInfo. aspx", function (responseText, textStatus, XMLHttpRequest ){

// ResponseText: Content returned by the request

// TextStatus: Request status: success, error, notmodified, timeout

// XMLHttpRequest: XMLHttpRequest object

});

 

DataType specifies the expected server response data type. By default, jQuery performs intelligent judgment. Possible types: "xml" "html" "text" "script" "json" "jsonp"

The biggest difference between jQuery. post and Jquery. get is that the former loads information through remote http post requests. The latter loads information through a remote http get request. The other parts are basically the same.

 

Instance:

Client --

[Html]
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqueryAjaxGetPost. aspx. cs" Inherits = "JqueryAjaxTest. JqueryAjaxGet" %>
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Jquery Ajax Test </title>
<% -- Introduce the Jquery library -- %>
<Script src = "Scripts/jquery-1.7.2.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// Bind events to buttons
 
$ ("# TestGet"). bind ("click", GetWithCallback );
$ ("# TestPost"). bind ("click", PostWithCallback );
});
 
 
// Test get and use the callback function
// Note: The get () method provides the callback function. This function has three parameters, representing the content returned by the request, the Request status, and the XMLHttpRequest object.
Function GetWithCallback (event ){
 
$. Get ("Data/GetServiceInfo. aspx", {"param": "TestGet-Callback"}, function (responseText, textStatus, XMLHttpRequest ){
 
$ ("# Result" ..html ("the callback function is at work. result:" + responseText );
 
});
}
 
// Test post and use the callback function
Function PostWithCallback (event ){
 
$. Post ("Data/GetServiceInfo. aspx", {"param": "TestPost-Callback"}, function (responseText, textStatus, XMLHttpRequest ){
 
$ ("# Result" ..html ("the callback function is at work. result:" + responseText );
 
});
}

</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

<Input id = "TestGet" type = "button" value = "test jquery. get"/>
<Input id = "TestPost" type = "button" value = "test jquery. post"/>
<Div id = "result">
</Div>
</Div>
</Form>
</Body>
</Html>

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqueryAjaxGetPost. aspx. cs" Inherits = "JqueryAjaxTest. JqueryAjaxGet" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Jquery Ajax Test </title>
<% -- Introduce the Jquery library -- %>
<Script src = "Scripts/jquery-1.7.2.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// Bind events to buttons

$ ("# TestGet"). bind ("click", GetWithCallback );
$ ("# TestPost"). bind ("click", PostWithCallback );
});


// Test get and use the callback function
// Note: The get () method provides the callback function. This function has three parameters, representing the content returned by the request, the Request status, and the XMLHttpRequest object.
Function GetWithCallback (event ){

$. Get ("Data/GetServiceInfo. aspx", {"param": "TestGet-Callback"}, function (responseText, textStatus, XMLHttpRequest ){

$ ("# Result" ..html ("the callback function is at work. result:" + responseText );

});
}

// Test post and use the callback function
Function PostWithCallback (event ){

$. Post ("Data/GetServiceInfo. aspx", {"param": "TestPost-Callback"}, function (responseText, textStatus, XMLHttpRequest ){

$ ("# Result" ..html ("the callback function is at work. result:" + responseText );

});
}
 
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

<Input id = "TestGet" type = "button" value = "test jquery. get"/>
<Input id = "TestPost" type = "button" value = "test jquery. post"/>
<Div id = "result">
</Div>
</Div>
</Form>
</Body>
</Html>

Server --

 

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
 
Namespace JqueryAjaxTest. Data
{
Public partial class GetMethodInfo: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
 
String param = "";
 
// Obtain parameters
If (! String. IsNullOrEmpty (HttpContext. Current. Request ["param"])
{
Param = HttpContext. Current. Request ["param"];
}

// Clear the buffer
Response. Clear ();
// Write the string to the response output stream
Response. Write ("Http Request Method:" + Request. HttpMethod. ToUpper () + "; the passed parameter is:" + param );
// Send the Current Buffer output to the client and stop executing this page
Response. End ();
 
}
}
}

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Namespace JqueryAjaxTest. Data
{
Public partial class GetMethodInfo: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

String param = "";

// Obtain parameters
If (! String. IsNullOrEmpty (HttpContext. Current. Request ["param"])
{
Param = HttpContext. Current. Request ["param"];
}

// Clear the buffer
Response. Clear ();
// Write the string to the response output stream
Response. Write ("Http Request Method:" + Request. HttpMethod. ToUpper () + "; the passed parameter is:" + param );
// Send the Current Buffer output to the client and stop executing this page
Response. End ();

}
}
}
 

Add:

 

Compare load and get:

 

$. Load () is the simplest way to get data from the server. It is almost equivalent to $. get (). The difference is that it is not a global function and it has an implicit callback function. When a response is detected successfully (for example, when textStatus is "success" or "notmodified"), $. load () sets the HTML content of the matching element to the returned data.

 

This is why $ ("# result"). load () is used in the previous instance (). In this example, $. get () is used ()

 

 


Author: shan9liang

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.