Javascript:调用C#codebehind字符串函数但返回当前页面的所有HTML

时间:2016-06-23 09:11:00

标签: javascript c# html asp.net pagemethods

我正在使用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>

1 个答案:

答案 0 :(得分:0)

检查此块

https://www.nuget.org/packages/HtmlAgilityPack

它将根据您输入的域

获取html代码