简体   繁体   English

Javascript:在字符串函数后面调用C#代码,但返回当前页面的所有HTML

[英]Javascript: Call C# codebehind string function but return all HTML of current page

I am using PageMethods to call C# function from Javascript. 我正在使用PageMethods从Javascript调用C#函数。

My C# function is return as String. 我的C#函数以String形式返回。 But after i called that function from Javascript it returned current page. 但是在我从Javascript调用该函数后,它返回了当前页面。

Here my code: 这是我的代码:

ASPX: 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 ASPX.CS

[WebMethod]
public static string returnStr4Frontend(string id)
{
    string reStr = string.Empty;

    reStr = "Your id is: " + id;

    return reStr;
}

When i run my page on browser, i received wrong result was (all html code of current page): 当我在浏览器上运行页面时,收到的错误结果是(当前页面的所有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>

Check this nugget 检查这个金块

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

it will get the html code depending on the domain you entered 它将根据您输入的域获取html代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM