Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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
Javascript 将参数从ajax函数传递到aspx.cs文件_Javascript_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

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

Javascript 将参数从ajax函数传递到aspx.cs文件,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,我已经用HTML、JS和AJAX创建了一些代码,但它并没有按照我所希望的方式工作 <script type="text/javascript"> function DeleteSelectedRecord(id) { alert(id); $.ajax({ type: "POST", url: "PrintTasks.aspx/DeleteRecord",

我已经用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>
里面有什么并不重要。最奇怪的是它没有读取我的参数

谢谢大家!

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!");
                }
            });
只需更改这行代码。它会起作用的

改为

第一个id是在.cs文件方法中设置的参数名称。。第二 一个是你要传递的价值

只需更改这行代码。它会起作用的

改为

第一个id是在.cs文件方法中设置的参数名称。。第二 一个是你要传递的价值


尝试如下所述:

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


尝试如下所述:

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

使用$.param()函数将数据发送到服务器。 代码如下:

       $.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!");
            }
        });
使用$.param()函数将数据发送到服务器。 代码如下:

       $.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!");
            }
        });

这一个仍然在创建一个匿名对象作为字符串!这一个仍然在创建一个匿名对象作为字符串!
 $.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!");
                }
            });
       $.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!");
            }
        });