Json 到 ASP.NET 当 JSON.stringify 时 MVC 控制器参数为空

Json to ASP.NET MVC Controller parameter is null when JSON.stringify

我正在尝试通过 MVC 控制器获取信息,方法是将 JSON 对象作为参数发送给它。

控制器方法如下所示:

public ActionResult GetLatestInfoLogs(InfoLogUserJson invoker, InfoLogUserJson affected) {
    // code
}

我有一个如下所示的服务器端模型:

public class InfoLogUserJson {
    public int id { get;set; }
    public int personId { get;set;}
    public int customerId { get;set; }
    public int rootUserId { get;set; }
    public string ip { get;set; }
    public bool unknown { get;set; }
}

我有一个如下所示的客户端脚本:

var InfoLogUser = function () {
    this.id = ko.observable(0);
    this.personId = ko.observable(0);
    this.rootUserId = ko.observable(0);
    this.customerId = ko.observable(0);
    this.unknown = ko.observable(false);
    this.ip = ko.observable(null);
}

InfoLogUser.prototype = (function() {
     return {
          toJSON: function() {
            return {
                personId: this.getPersonId(),
                rootUserId: this.getRootUserId(),
                customerId: this.getCustomerId(),
                id: this.getId(),
                unknown: this.getUnknown(),
                ip: this.getIp()
             }
         }
     }
}());

在 javascript 视图模型中,我正在尝试这样做:

            var infoLogUser = new InfoLogUser();
            infoLogUser.personId(1234);

            $.ajax({
                url: '/Whatever/GetLatestInfoLogs',
                data: {
                    invoker: JSON.stringify(infoLogUser.toJSON()),
                    affected: null
                },
                dataType: 'application/json; charset: utf-8',
                type: 'GET',
                success: function(_infoLogs) {
                    alert('yay');
                }
            });

在我的网络日志中,我得到以下信息: 查询字符串参数:

调用者:{"personId":1234,"rootUserId":0,"customerId":0,"id":0,"unknown":false,"ip":空} 受影响:

但是,当它命中 MVC 控制器中的 GetLatestInfoLogs 方法时,调用程序参数始终为 null。如果我从 ajax 请求中删除 JSON.stringify 调用程序参数不为空,但其中未设置任何值。

我真的不知道发生了什么,所以希望你们中的任何人都知道发生了什么? :)

您无需创建自己的 .toJSON() 方法。

只需这样做:

invoker: JSON.stringify(infoLogUser)

您还需要确保设置内容类型,即

contentType: 'application/json'

我还注意到你的数据类型应该是:

dataType: 'json'

整个调用将变为:

    var infoLogUser = new InfoLogUser();
    infoLogUser.personId(1234);

    $.ajax({
        url: '/Whatever/GetLatestInfoLogs',
        data: {
            invoker: JSON.stringify(infoLogUser),
            affected: null
        },
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
        success: function(_infoLogs) {
            alert('yay');
        }
    });

更新 要使 json 有效,您需要将 invoker & affected 更改为字符串,即

 data: {
         "invoker": JSON.stringify(infoLogUser),
         "affected": null
        }

试试这个

$.ajax({
        url: '/Whatever/GetLatestInfoLogs',
        type: 'POST',
        data: {
            invoker: JSON.stringify(infoLogUser),
            affected: null
        },
        contentType: 'application/json',

        success: function(_infoLogs) {
            alert('yay');
        }
    });

已更新

var data = {invoker: infoLogUser, affected: null};
$.ajax({
            url: '/Whatever/GetLatestInfoLogs',
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'application/json',

            success: function(_infoLogs) {
                alert('yay');
            }
        });