Home  >  Article  >  Web Front-end  >  Jquery asynchronous request data example code_jquery

Jquery asynchronous request data example code_jquery

WBOY
WBOYOriginal
2016-05-16 17:57:481354browse

1. Jquery requests data from the aspx page
Front page JS code:

Copy code The 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 code The code is as follows:




Backend cs code:
Copy code The 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"] ""}");//Piece the data into Json
Response.End();
}
}

2. Jquery requests data from the WebService page
Copy the code The 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);
}
});
});


WebService code
Copy code The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
// /
/// Summary description for WebService
///

[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;
}
}

3. Jquery requests data from ashx The same
Js code as on the page:
Copy code The code is as follows:

$( "#Button3").bind("click", function () {
$.ajax({
type: "post",
url: "Handler.ashx",
data: " name=" $("#Text1").val(),
success: function (result) {
alert(result.msg);
}
});
}) ;

Backend code:
Copy code The 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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn