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