没有 PageMethods:
首先你需要引用一个jquery库
http://jquery.com/
然后在服务器端,您需要一个返回一些数据的函数,例如:
参考需求:
使用 System.Web.Services;
[WebMethod]
public static string Test(string id)
{
// do ...
return "data";
}
在页面上你需要类似的东西:
<script type="text/javascript">
$(document).ready(
function () {
$.ajax({
type: "POST",
url: "Default.aspx/Send",
data: "{id : '" + "data to send" + "'}",
contentType: "application/json; charset=utf-8",
cache: false,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error occured textStatus=" + textStatus + " errorThrown=" + errorThrown);
},
success: function (msg) {
// Output comes here
}
});
});
</script>
积分:
服务器端函数应该是公共的和静态的,并且具有属性 WEBMETHOD
- 使用ajax时调用参数名必须匹配即
编辑:
加载图片非常简单:
在客户端创建一个像这样的空图像
<img src="" alt="Please Wait Loading" id="test" />
并在输出中返回图像 url,如 'load.png'
在 JSON 的输出中使用这个:
success: function (msg) {
alert(msg.d);
$('#test').attr("src", msg.d);
}
请务必在末尾写上 msg.d note .d 字。
如果你有任何问题,请告诉我
作为数据:“{id : '”+“要发送的数据”+“'}”,
“id”但等于服务器端参数名称
使用 PageMethods
服务器端的东西保持不变,但在客户端你不需要 jquery
你需要:
-
ScriptManager 启用了页面方法,例如
<asp:ScriptManager EnablePageMethods="true" />
要调用一个函数,你需要类似的东西
PageMethods.Send("param name" , result);
发送 = 函数名称
result 是在 result 声明为 like 后将执行的函数的名称
function result(output)
{
alert("function return value");
}