简体   繁体   English

将参数从ajax函数传递到aspx.cs文件

[英]Pass the argument from ajax function to aspx.cs file

I have created some code in HTML, JS and AJAX, yet it does not work how I want it to. 我已经用HTML,JS和AJAX创建了一些代码,但是按我的意愿无法正常工作。

<script type="text/javascript">
function DeleteSelectedRecord(id) {
            alert(id);
            $.ajax({
                type: "POST",
                url: "PrintTasks.aspx/DeleteRecord",
                data: '{"id":"' + id + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("Deleted!");
                },
                failure: function () {
                    alert("Failure!");
                }
            });

        }
    function CreateATableFromDataBase() {
        var deserializedData = '<%=serializedList %>';
            deserializedData = JSON.parse(deserializedData);
            for (var i = 0; i < deserializedData.length; i++) {
                document.write(
                    "<tr>" +
                    "<th scope=\"row\">" + i.toString() + "</th>" +
                    "<th>" + deserializedData[i]["Name"] + "</th>" +
                    "<th>" + deserializedData[i]["Where"] + "</th>" +
                    "<th>" + deserializedData[i]["Destination"] + "</th>" +
                    "<th><input type=\"button\" class=\"btn btn-primary\" onclick=\"DeleteSelectedRecord(" + deserializedData[i]["Id"] + ")\" value=\"Delete\"/></th>" +
                    "</tr>"
                );
            }
    }
</script>

The second function passes the argument "id" to the first one. 第二个函数将参数“ id”传递给第一个。 I have used alert method so as to check, whether at least it works properly. 我已使用警报方法来检查是否至少可以正常工作。 It does. 是的 The problem starts when I want to pass the parameter to my super method called DeleteRecord in PrintTasks.aspx.cs file... 当我要将参数传递给PrintTasks.aspx.cs文件中名为DeleteRecord的超级方法时,问题就开始了。

[System.Web.Services.WebMethod]
    public static void DeleteRecord(int id)
    {
        very sophisticated code...
    }

It is not important what is inside. 内部的内容并不重要。 The most curious thing is that it does not read my parameter. 最奇怪的是它不读取我的参数。

Thank you! 谢谢!

Just change This Line of code. 只需更改此代码行。 It will work 会工作的

 data: '{"id":"' + id + '"}',

Change to 改成

 data: { id:id},

First id is the parameter name you set in .cs file method.. and second one is value you are going to pass... 第一个ID是您在.cs文件方法中设置的参数名称。.第二个ID是您要传递的值...

Try as mentioned below : 尝试如下所述:

$.ajax({
                type: "POST",
                url: "PrintTasks.aspx/DeleteRecord?id=" + id,
                success: function () {
                    alert("Deleted!");
                },
                failure: function () {
                    alert("Failure!");
                }
            });

Or 要么

 $.ajax({
                type: "POST",
                url: "PrintTasks.aspx/DeleteRecord",
                data: JSON.stringify({id:id}),
                contentType: "application/json;",
                dataType: "json",
                success: function () {
                    alert("Deleted!");
                },
                failure: function () {
                    alert("Failure!");
                }
            });

Use $.param() function for sending data to server. 使用$ .param()函数将数据发送到服务器。 Here is the code: 这是代码:

       $.ajax({
           type: "POST",
            url: "PrintTasks.aspx/DeleteRecord",
            data: $.param({"id": id}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert("Deleted!");
            },
            failure: function () {
                alert("Failure!");
            }
        });
Try by changing your ajax code
            $.ajax({
                type: "POST",
                url: "PrintTasks.aspx/DeleteRecord",
                data: '{"id":"' + ParseInt(id) + '"}',//change your code here
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("Deleted!");
                },
                failure: function () {
                    alert("Failure!");
                }
            });

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

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