得到“JSON 请求太大而无法反序列化”

问题描述

您必须将 maxJsonLength属性调整为更高的值web.config才能解决此问题。

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
        </webServices>
    </scripting>
</system.web.extensions>

aspnet:MaxJsonDeserializerMembers在 appSettings 中设置更高的值:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

解决方法

我收到此错误:

JSON 请求太大而无法反序列化。

这是发生这种情况的场景。我有一个国家类别,其中包含该国家/地区的运输港口列表

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Port> Ports { get; set; }
}

我在客户端使用 KnockoutJS 进行级联下拉。所以我们有两个下拉列表,第一个是国家,第二个是那个国家的港口。

到目前为止一切正常,这是我的客户端脚本:

var k1 = k1 || {};
$(document).ready(function () {

    k1.MarketInfoItem = function (removeable) {
        var self = this;
        self.CountryOfLoadingId = ko.observable();
        self.PortOfLoadingId = ko.observable();
        self.CountryOfDestinationId = ko.observable();
        self.PortOfDestinationId = ko.observable();  
    };

    k1.viewModel = function () {
        var marketInfoItems = ko.observableArray([]),countries = ko.observableArray([]),saveMarketInfo = function () {
                var jsonData = ko.toJSON(marketInfoItems);
                $.ajax({
                    url: 'SaveMarketInfos',type: "POST",data: jsonData,datatype: "json",contentType: "application/json charset=utf-8",success: function (data) {
                        if (data) {
                            window.location.href = "Fin";
                        } else {
                            alert("Can not save your market information now!");
                        }

                    },error: function (data) { alert("Can not save your contacts now!"); }
                });
            },loadData = function () {
                $.getJSON('../api/ListService/GetCountriesWithPorts',function (data) {
                    countries(data);
                });
            };
        return {
            MarketInfoItems: marketInfoItems,Countries: countries,LoadData: loadData,SaveMarketInfo: saveMarketInfo,};
    } ();

The problem occurs when a country like China is selected,which has lots of
ports. 因此,如果您的阵列中有 3 或 4 次“中国”,我想将其发送到服务器进行保存。发生错误。

我应该怎么做才能解决这个问题?