【问题标题】:Pass Javascript Array of Objects to C# Codebehind将对象的 Javascript 数组传递给 C# 代码隐藏
【发布时间】:2014-12-28 03:03:45
【问题描述】:

我在将我的 Javascript 对象数组发布到 C# Codebehind 时遇到一些问题。我遵循了一个简单的教程,并认为这会很好地工作,但我在 PassThings 中的 C# 代码隐藏断点从未被命中。

我已尝试将 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

代码隐藏

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; }
    }

}

有人看到我做错了吗?

感谢您的宝贵时间。

【问题讨论】:

  • 需要定义PassThings函数web方法
  • @Satpal 我在 c# 代码隐藏中的 PassThings 上方添加了 [WebMethod],但仍然没有命中回发断点,我还添加了 [System.Web.Script.Services.ScriptService]
  • 尝试在ajax调用中添加traditional : true属性
  • @EhsanSajjad 没有效果:(
  • 失败回调可能应该是“错误”。 api.jquery.com/jquery.ajax

标签: javascript c# asp.net ajax json


【解决方案1】:

在url 中通过页面传递正确的 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] 装饰,它应该是public 和static:

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

【讨论】:

  • ...你太棒了:)谢谢。由于我的方法现在是静态的,有没有更简单的方法可以用 PageMethod 做到这一点?
  • 没有 ajax 调用我们必须将其设为静态并使用 webmethod 进行装饰,您可以使用扩展名为 .ashx 的 asp.net 通用处理程序
猜你喜欢
  • 2018-12-23
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多