Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从javascript函数调用asp.net函数_C#_Javascript_Jquery_Asp.net - Fatal编程技术网

C# 从javascript函数调用asp.net函数

C# 从javascript函数调用asp.net函数,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我想从传递字符串值的javascript函数调用asp.net函数 这是asp.net函数: [System.Web.Services.WebMethod] public static string InsertData(string ID) { string source = "Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30"; using(SqlConnection co

我想从传递字符串值的javascript函数调用asp.net函数

这是asp.net函数:

[System.Web.Services.WebMethod]     
public static string InsertData(string ID)
{
    string source = "Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30";
    using(SqlConnection con = new SqlConnection(source))
    {
        using (SqlCommand cmd = new SqlCommand("Insert into Book (Name) values(@Name)", con))       {
            con.Open();
            cmd.Parameters.AddWithValue("@Name", ID);
            cmd.ExecuteNonQuery();
            return "True";
        }
    }
}
所以我想从javascript函数调用这个函数,它将字符串值“ID”传递给asp.net函数

这是我使用的javascript函数

 function CallMethod() {
            PageMethods.InsertData("hello", CallSuccess, CallError);

        }

        function CallSuccess(res) {
            alert(res);
        }

        function CallError() {
            alert('Errorrr');
        }
我从这里叫它

<body>
        <header>        
        </header>       
        <div class="table"  id="div1" > </div>                      
        <form id="Form1" runat="server">
            <asp:Button id="b1" Text="Submit" runat="server" onclientclick="CallMethod();return false;"/>
            <asp:ScriptManager enablepagemethods="true" id="ScriptManager1" runat="server"></asp:ScriptManager>
        </form> 

    </body>


因此,我有一个按钮,单击一次,我想将“Hello”行添加到我的表中,但什么都没有发生,调用错误函数调用您可以在javascript中从ajax调用调用web方法。您需要为要调用的函数设置url参数值,并且可以在json格式的数据参数中传递值。 像这样的
数据:{parametername:'VALUE'}

 <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: "YourPage.aspx/YouPageMethod",
                data: "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    alert('Method Called Sucess fully');
                },
                error: function (result) {
                    alert("error " + result);
                }
            });
        });
    </script>

$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“YourPage.aspx/YouPageMethod”,
数据:“,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(结果){
警报(“完全调用成功的方法”);
},
错误:函数(结果){
警报(“错误”+结果);
}
});
});

您可以使用PageMethod调用


函数callInsert(){
插入日期(id,OnSucceeded,OnFailed);
}
函数onSucceed(响应){
警报(响应);
}
函数OnFailed(错误){
警报(错误);
}   
/*调用此javascript函数*/
callInsert();

首先需要在页面中包含Jquery。然后需要添加以下Javascript代码

 <script type="text/javascript">
  var id ="5";  //your id will be stored here
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "YourPage.aspx/InsertData"  + "&?id="  ,
            data: "",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                alert('Success');
            },
            error: function (xhr, request, status) {
                 alert(xhr.responseText);
            }
        });
    });
</script>

var id=“5”;//您的id将存储在此处
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
url:“YourPage.aspx/InsertData”+“&?id=”,
数据:“,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(结果){
警惕(“成功”);
},
错误:函数(xhr、请求、状态){
警报(xhr.responseText);
}
});
});

您的.aspx页面中需要有scriptManager

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

之后,您可以使用

    <script type="text/javascript">
    function func(){
    PageMethods.InsertData(id,success_msg,failure_msg);
    }
    </script>

函数func(){
插入数据(id、成功消息、失败消息);
}

所以当文档准备好时,它将从`MyPage.aspx.cs`调用函数(
“YouPageMethod”
)。在我的例子中,我的asp.net函数被调用为
InsertData
,它在WebForm1.aspx.cs
函数CallMethod(){PageMethods.InsertData(“hello”,CallSuccess,CallError);}函数CallSuccess(res){alert(res)}函数CallError(){alert('Errorrr');}
您可以更新您问题中的代码。其他人也可以看到它,我可以从按钮单击调用CallMethod()函数-
onclientclick=“CallMethod();“
onclientclick=“CallMethod();返回false;“添加return false,函数调用其他方式,你的页面将发布backok,因此我认为它在Jquery和PageMethod两种方式下都能工作。问题是,它在两种方式下都会向我发送失败消息,我不知道为什么。asp.net函数似乎构造正确。我有我需要的所有内容,同样的故事表中没有添加行。你绝对正确。”在验证之前,如果你知道给定的字符串输入是正确的格式,IE不是一个攻击有效载荷,而是盲目地将内容插入到数据库中。这是一个等待发生的XSS攻击向量。
    <script type="text/javascript">
    function func(){
    PageMethods.InsertData(id,success_msg,failure_msg);
    }
    </script>