我正在使用PageMethods从Javascript调用C#函数。
我的C#函数以字符串形式返回。 但是在我从Javascript调用该函数后,它返回了当前页面。
这是我的代码:
ASPX:
<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<script>
function testPageMethods(){
var id = 45;
PageMethods.returnStr4Frontend(id, onSucess, onError);
}
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(has error);
}
testPageMethods();
</script>
</form>
ASPX.CS
[WebMethod]
public static string returnStr4Frontend(string id)
{
string reStr = string.Empty;
reStr = "Your id is: " + id;
return reStr;
}
当我在浏览器上运行我的页面时,我收到了错误的结果(当前页面的所有HTML代码):
<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<script>
function testPageMethods(){
var id = 45;
PageMethods.returnStr4Frontend(id, onSucess, onError);
}
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(has error);
}
testPageMethods();
</script>
</form>