传递给javascript类对象后面的代码序列化

时间:2012-03-18 11:24:38

标签: c# javascript json

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 () {  }
        })

[的WebMethod]         public static string Getir(string id,string Binis,string Inis)         {             string a = id;             string b = HttpUtility.HtmlDecode(id);             return null;         }

问题是javascript函数无法获取序列化的参数(Uncaught SyntaxError:Unexpected token ILLEGAL)。如何才能完成这项工作?

问题是,当javascript函数运行时这不起作用它不能参数,怎么能完全不做这个工作

1 个答案:

答案 0 :(得分:0)

试试这样:

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