前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AdmanageSystem.WebForm1" %>
<!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></title>
<script src="http://localhost:15061/Scripts/jquery-1.8.js" type="text/javascript"></script>
<script type="text/javascript">
function clickMe() {
document.getElementById("lb2").innerText = "Hello world";
document.getElementById("lb3").innerText = "Hello world3";
return;
}
function myClick() {
$.ajax({
type: "post",
url: "WebForm1.aspx/takeCellCode",
//data: "{'cellPhoneNum':'" + cell.value + "','userName':'" + userName.value + "','ipAddress':'" + ipAddress.value + "'}",
data: "{'cellPhoneNum':'13254682354','userName':'test','ipAddress':'127.0.0.1'}",
//data: "cellPhoneNum=13254682354&userName=test&ipAddress=127.0.0.1",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// $("#bindCellTempValidateCode").val(data.d[0]);
// curCountForBindCell = countForBindCell;
// $("#takeCellForBindCellBtn").attr("disabled", "true");
// $("#takeCellForBindCellBtn").val("请在" + curCountForBindCell + "秒内输入");
// InterValObjForBindCell = window.setInterval(SetRemainTimeForBindCell, 1000);
// myClick(cell.value, data.d[0]);
//alert(data.d);
//alert(data.d[0]);
//alert(data.d[1]);
//alert(data.d[2]);
$.each(data, function (k, v) {
alert(k);
alert(v);
});
},
error: function (err) {
alert(err);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- 不使用label,使用span或服务器端label控件 <label id="lb1">我是原来的</label>--%>
<asp:label id="lb2" runat="server" Text="我是原来的"></asp:label>
<span id="lb3">我是原来的1</span>
<span id="Span1">我是原来的2</span>
<span id="Span2">我是原来的3</span>
<span id="Span3">我是原来的4</span>
<span id="Span4">我是原来的5</span>
<span id="Span5">我是原来的6</span>
<input type="button" οnclick="clickMe()" value="我是按钮,按我一下" />
<input type="button" οnclick="myClick()" value="我是ajax方法" />
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace AdmanageSystem
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//获取手机验证码
[WebMethod]
public static List<string> takeCellCode(string cellPhoneNum, string userName, string ipAddress)
//public static string takeCellCode(string cellPhoneNum, string userName, string ipAddress)
{
try
{
List<string> list = new List<string>();
//TODO:手机验证码
string code = (new Random()).Next(1000, 9999).ToString();
list.Add(cellPhoneNum+",");
list.Add(userName + ",");
list.Add(ipAddress + ",");
return list;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}