第一个答案的示例代码;
<script type=”text/javascript”>
function Click() {
PageMethods.SomeMethod(OnSucceeded, OnFailed);
}
function OnSucceeded(result, userContext, methodName) {
$get(‘Label1′).innerHTML = result;
}
function OnFailed(error, userContext, methodName) {
$get(‘Label1′).innerHTML = “Error text”;
}
</script>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePageMethods=”true”></asp:ScriptManager>
<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>
<asp:Button ID=”Button1″ runat=”server” OnClientClick=”Click();return false;” Text=”Button” />
在aspx页面代码后面;
[WebMethod]
public static string SomeMethod()
{
//return cities as Json as suggested by the first answer
}