Jquery asynchronous request data instance code

Source: Internet
Author: User

I. Jquery requests data from the aspx page
Front-end page JS Code:
Copy codeThe Code is as follows:
$ ("# Button1"). bind ("click", function (){
$. Ajax ({
Type: "post ",
Url: "default. aspx ",
Data: "name =" + $ ("# Text1"). val (),
Success: function (result ){
Alert (result. msg );
}
});
});

Copy codeThe Code is as follows:
<Input id = "Text1" type = "text" value = 'zhang san'/>
<Input id = "Button1" type = "button" value = "Submit"/>

Backend cs code:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["name"]! = Null)
{
Response. ContentType = "text/json ";
Response. Write ("{\" msg \ ": \" "+ Request [" name "] +" \ "}"); // splice the data into Json
Response. End ();
}
}

Ii. Jquery requests data from the WebService page
Copy codeThe Code is as follows:
$ ("# Button2"). bind ("click", function (){
$. Ajax ({
Type: "post ",
ContentType: "application/json ",
Url: "WebService. asmx/HelloWorld ",
Data: "{name: '" + $ ("# Text1"). val () + "'}",
Datatype: "json ",
Success: function (result ){
Alert (result. d );
}
});
});
<Input id = "Button2" type = "button" value = "Submit to WebService"/>

WebService code
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
/// <Summary>
/// Summary description for WebService
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP. net ajax, uncomment the following line.
[System. Web. Script. Services. ScriptService]
Public class WebService: System. Web. Services. WebService {
Public WebService (){
// Uncomment the following line if using designed components
// InitializeComponent ();
}
[WebMethod]
Public string HelloWorld (string name ){
Return "Hello World" + name;
}
}

Iii. Jquery requests data from ashx and returns the same data to the page
Js Code:
Copy codeThe Code is as follows:
$ ("# Button3"). bind ("click", function (){
$. Ajax ({
Type: "post ",
Url: "Handler. ashx ",
Data: "name =" + $ ("# Text1"). val (),
Success: function (result ){
Alert (result. msg );
}
});
});

Background code:
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "Handler" %>
Using System;
Using System. Web;
Public class Handler: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/json ";
Context. Response. Write ("{\" msg \ ": \" Hello World "+ context. Request [" name "] +" from handler. ashx \"}");
Context. Response. End ();
}
Public bool IsReusable {
Get {
Return false;
}
}
}

Code download

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.