【问题标题】:Javascript Object pass to ASP.NET HandlerJavascript 对象传递给 ASP.NET 处理程序
【发布时间】:2012-11-05 15:45:12
【问题描述】:

如何将 JavaScript 对象传递给 ASP.NET 处理程序并解析值?

我创建了一个复杂类型的对象,例如:

function AccountObjCreate() {
var AccountsView = {};
AccountsView.Username = null;
AccountsView.Email = null;
AccountsView.Password = null;

return AccountsView;
}

然后像这样填充那个对象:

var aView = AccountObjCreate();
aView.Username = $('#tbUserName').val().trim();
aView.Email = $('#tbEmail').val().trim().toLowerCase();
aView.Password = $('#tbPassword').val().trim();

然后我打电话给:

$.post("/Handlers/AccountHandler.ashx", { obj: aView },
        function (results) {
            if (results.isSuccess) {
                alert(results.msg);
            } else {
                alert(results.msg);
            }
        }, "json");

当我在控制台中查看它时,我将 aView 中的所有数据都视为 json。

我的 ASP.NET 处理程序页面是

context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
string obj = context.Request["obj"];

但是 obj 是 NULL。

【问题讨论】:

    标签: c# javascript asp.net json httphandler


    【解决方案1】:

    jQuery 不会自动为您字符串化对象。你必须自己做。

    下载这个:https://github.com/douglascrockford/JSON-js

    将脚本引用添加到您的 HTML 并将您的 $.post 调用更改为:

    $.post("/Handlers/AccountHandler.ashx", { obj: JSON.stringify(aView, null, 2) }...
    

    ContentType 也不正确。我只是将其保留为默认值。虽然您将以这种方式提交 JSON 数据,但 ContentType 本身不是 JSON。内容类型将是常规发布数据。这是帖子数据:

    obj={"Username":"MyUsername", "Password":"MyPassword"...
    

    这是 JSON

    {"obj":{"Username":"MyUsername", "Password":"MyPassword"...
    

    有时会让人感到困惑。

    更好的方法是研究 ASP.Net MVC。控制器方法能够自动将 JSON 对象反序列化为 .Net 模型,因此您可以在客户端代码上执行此操作:

    $.post("/Handlers/AccountHandler.ashx", JSON.stringify({ view: aView }, null, 2) }...
    

    编写一个匹配aView结构的.Net类:

    class Account { 
        public string Username { get; set; } 
        public string Password { get; set; }
        public string Email { get; set; }
    }
    

    然后像这样定义一个控制器方法:

    public JsonResult SaveAccount(Account view)
    

    让事情变得简单多了。 :)

    【讨论】:

    • 更简单,但对于仍然仅在 .NET 2.0 上运行的糟糕项目,juan.facorro 的解决方案是最好的选择。
    【解决方案2】:

    为了将对象传递到服务器,您应该将其序列化为字符串(您可以使用描述的方法here 或包含用于 javascript 的 JSON 库here),然后使用 .NET @ 反序列化它987654323@班级。

    JS 代码。

    var data = {
        obj: JSON.stringify(aView)
    };
    $.post("/Handlers/AccountHandler.ashx", data, function(results) {
        if (results.isSuccess) {
            alert(results.msg);
        } else {
            alert(results.msg);
        }
    }, "json");​
    

    然后在服务器处理程序上,您可以使用提到的JavaScriptSerializer 类解析 JSON 字符串。

    public class AccountsView {
        public string Username;
        public string Email;
        public string Password;
    }
    
    public void ProcessRequest(HttpContext context)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string value = context.Request["obj"];
        var aView = serializer.Deserialize(value, typeof(AccountsView));
    
        context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = Encoding.UTF8;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多