【问题标题】:pass to javascript class object is serialized in code behind传递给 javascript 类对象在后面的代码中被序列化
【发布时间】:2023-04-01 04:20:02
【问题描述】:
JavaScriptSerializer jss = new JavaScriptSerializer();
                var seferim = jss.Serialize(sefer);
                string asds = seferim.ToString();
                asds = HttpUtility.HtmlEncode(asds);

function otobusGetir(id, b, i) {
        id = encodeURI(id);

        $.ajax({
            type: 'POST',
            url: 'BiletSatis.aspx/Getir',
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            data: '{"id":"' + id + '","Binis":"' + b + '","Inis":"' + i + '"}',
            success: function () { },
            error: function () {  }
        })

[网络方法] 公共静态字符串Getir(字符串ID,字符串Binis,字符串Inis) { 字符串 a = id; 字符串 b = HttpUtility.HtmlDecode(id); 返回空值; }

问题是javascript函数不能接受序列化的参数(Uncaught SyntaxError: Unexpected token ILLEGAL)。我怎样才能完全完成这项工作?

问题是这不起作用,而 javascript 函数运行它不能接受参数,如何完全完成这项工作

【问题讨论】:

  • 你也可以添加javascript代码吗?您发布的 json 是有效的。您应该能够在 javascript 中反序列化。
  • 我不明白你想做什么。你的 json 文字看起来是正确的。 seferim 应该能够毫无问题地作为参数传递给任何 js 函数。你能显示你的代码被破坏了吗?
  • “我怎样才能将像上面这样的数据发送到 javascript 的类”你能改写一下吗?我认为这是问题的主题,但我不确定。
  • 函数获取参数时导致的问题,函数无法获取并说 Uncaught SyntaxError: Unexpected token ILLEGAL –

标签: c# javascript json


【解决方案1】:

试试这样:

function otobusGetir(id, b, i) {
    $.ajax({
        type: 'POST',
        url: 'BiletSatis.aspx/Getir',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        data: JSON.stringify({ id: id, Binis: b, Inis: i }),
        success: function () { },
        error: function () {  }
    });
}

并调用函数:

otobusGetir(123, 'foo bar', 'baz');

和页面方法:

[WebMethod]
public static string Getir(string id, string Binis, string Inis)
{
    // don't use any Html decoding here. The parameters will
    // already be properly formatted so you can use them directly

    return null;
}

【讨论】:

  • 函数获取参数时导致的问题,函数无法获取并说 Uncaught SyntaxError: Unexpected token ILLEGAL
  • 对不起,我听不懂你在说什么。
  • 对,您似乎在尝试调用 ASPX 页面方法。我已经更新了我的答案。
  • 你的东西不能工作,因为 javascript 不能接受参数,这个方法可以接受参数,但是在 aspx 页面方法中,解码前和解码后解码是一些我该如何修复它
  • 我认为语言障碍太大,我无法理解您在说什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 2011-12-12
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多