Default.aspx :
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!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>jquery_ajax获得后台返回值</title>
< script type=”text/javascript” language=”javascript” src=”js/jquery-1.4.4.min.js”></script>
< /head>
< body>
< form id=”form1″ runat=”server”>
<div>
< input id=”btnOK” type=”button” value=”按钮” />
< /div>
< script type=”text/javascript” language=”javascript”>
$(function() {
$(“#btnOK”).click(function() {
$.ajax({
type: “Post”,
url: “Default.aspx/HelloWord”, //’/'前表示调用的页面,之后表示调用的方法
//方法传参的写法一定要对,str1为形参的名字,str2为第二个形参的名字
data: “{‘str1′:’Hello’,'str2′:’Word’}”,
contentType: “application/json; charset=utf-8″,
dataType: “json”,
success: function(data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function(err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
< /script>
< /html>
Default.aspx.cs:
using System.Web.Script.Services;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
[WebMethod]
public static string HelloWord(string str1,string str2)
{
return str1 + str2;
}
}