繁体   English   中英

JSON.stringify时,Json to ASP.NET MVC Controller参数为null

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

我试图通过向其发送JSON对象作为参数,通过MVC控制器获取信息。

控制器方法如下所示:

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”:null}受影响:

但是,当它在MVC控制器中命中GetLatestInfoLogs方法时,调用者参数始终为null。 如果我从ajax请求中删除JSON.stringify,则调用者参数不为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');
            }
        });

您无需为.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 invokeraffected为字符串即

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM