Jquery 는 Asp. Net WebService 와 form 을 Handler 방식 으로 호출 합 니 다.

20780 단어 webservice
Html 코드:


View Code
<!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>
<title> </title>
</head>
<script type="text/javascript" src="javsscript/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$(
"#send").click(function() {
var username = $("#username").val();
var age = $("#age").val();
$.ajax({
type:
"post", // WebService Post
contentType:
"application/json;utf-8", //webservice Json
url:
"IbeaconWebService.asmx/SendEmail", // webservice ---url/
data:
"{username:'" + username + "',age:'" + age + "'}", //
datatype:
"json",
success:
function(msg) { // msg
$(
"#message").html(msg.d);// div
}
});
return false;
});
});
</script>
<body>
<form action="IbeaconHandler.ashx" method="get">
<table>
<tr>
<td>

</td>
<td>
<input id="username" name="username" type="text" />
</td>
</tr>
<tr>
<td>

</td>
<td>
<input id="age" name="age" type="text" />
</td>
</tr>
<tr>
<td>
<input type="button" value="ajax " id="send" />
</td>
<td>
<input type="submit" value="form " onclik="submit()" />
</td>
</tr>
<tr>
<td colspan="2">
<div id="message">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>

페이지 캡 처:
Jquery调用Asp.Net WebService和form 提交到Handler方式
배경 웹 서비스 코드:


View Code
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
///IbeaconWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// ASP.NET AJAX Web , 。
[System.Web.Script.Services.ScriptService]
public class IbeaconWebService : System.Web.Services.WebService {

public IbeaconWebService () {

//
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string SendEmail(string username,int age) {
return " ! :"+username+""+age;
}

}

Handler 코드:


View Code
 1 <%@ WebHandler Language="C#" Class="IbeaconHandler" %>
2
3 using System;
4 using System.Web;
5
6 public class IbeaconHandler : IHttpHandler {
7
8 public void ProcessRequest (HttpContext context) {
9 context.Response.ContentType = "text/html";
10 string name = context.Request["username"].ToString();
11 string age = context.Request["age"].ToString();
12 //context.Response.Write(" :"+name);
13 context.Response.Write(" ! :"+name+""+age);
14 }
15
16 public bool IsReusable {
17 get {
18 return false;
19 }
20 }
21
22 }

좋은 웹페이지 즐겨찾기