将 JavaScript 数组对象传递到 C# Codebehind

3
我遇到了一些问题,无法将JavaScript对象数组发布到C# Codebehind。 我按照一个简单的教程进行操作,认为这应该能很好地工作,但我的C# Codebehind中 PassThings 的断点从未被激活。
我尝试将URL更改为“Default.aspx / PassThings”,但它仍然没有发布到我的后端代码,错误警报显示“[object object"]”
以下是我的客户端代码:
Default.aspx
脚本
<script>

    function Save() {
        $(document).ready(function () {
            var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
        ];

                things = JSON.stringify({ 'things': things });

                $.ajax({
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    type: 'POST',
                    url: '/PassThings',
                    data: things,
                    success: function () {
                        alert("success");
                    },
                    error: function (response) {
                        alert(response);
                    }
                });
            });

    }

</script>

Html

<input type="button" value="Pass Things" onclick="JavaScript: Save();">

Default.aspx.cs

Codebehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;

[System.Web.Script.Services.ScriptService]
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public void PassThings(List<Thing> things)
    {
        var t = things;
    }

    public class Thing
    {
        public int Id { get; set; }
        public string Color { get; set; }
    }

}

有人看到我做错了什么吗?

谢谢你的时间。


1
您需要定义PassThings函数的Web方法。 - Satpal
@Satpal 我在C#的代码后台PassThings上方添加了[WebMethod],但仍然没有触发postback断点。我还添加了[System.Web.Script.Services.ScriptService]。 - clamchoda
@EhsanSajjad 没有任何效果 :( - clamchoda
尝试使用console.log(response);而不是alert。在您的开发控制台中,您应该能够查看对象的属性。 - Dan
错误回调函数的参数是 jqXHR jqXHR, String textStatus, String errorThrown。所以你目前看到的是 jqXHR 对象。但你可能更关心的是 errorThrown。 - Dan
显示剩余4条评论
1个回答

6
url中传递正确的页面链接。假设PassThings方法在Default.aspx页面的代码后台文件中,则如果脚本代码写在Default.aspx中,则必须传递url: Default.aspx/PassThings
如果脚本在位于Scripts文件夹中的单独js文件中,则您必须返回一个目录并编写:url: ../Default.aspx/PassThings
$(document).ready(function () {
    var things = [{
        id: 1,
        color: 'yellow'
    }, {
        id: 2,
        color: 'blue'
    }, {
        id: 3,
        color: 'red'
    }];

    things = JSON.stringify({
        'things': things
    });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: 'Default.aspx/PassThings',
        data: things,
        success: function () {
            alert("success");
        },
        error: function (response) {
            alert(JSON.stringify(response));
        }
    });
});

在代码后台,你的方法应该被标记为 [WebMethod],并且它应该是publicstatic的:

    [WebMethod]
    public static void PassThings(List<Thing> things)
    {
        var t = things;
    }

你很棒 :) 谢谢。由于我的方法现在是静态的,是否有更简单的方法可以使用PageMethod来做到这一点? - clamchoda
对于 AJAX 调用,我们必须将其设置为静态并使用 WebMethod 进行装饰。您可以使用 ASP.NET 的通用处理程序,它的扩展名为 .ashx - Ehsan Sajjad
请查看此文章:http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev - Ehsan Sajjad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接