maxJsonLength属性指的是什么? [英] What does the maxJsonLength property refer to?

查看:74
本文介绍了maxJsonLength属性指的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Web窗体项目中,我正在使用SQL存储过程加载jqGrid,并在其中将数据作为json返回.在初始设置和测试期间,我返回了85行数据.然后,我更改了导致未返回到网格中的1,868行的参数.

In a web forms project, I am loading a jqGrid using a SQL stored procedure where I return the data as json. During initial setup and testing, I was returning 85 rows of data. I then changed the parameters which caused 1,868 rows to be returned, except it was not displaying in the grid.

在Firebug中调试时,我看到了错误字符串的长度超过了在maxJsonLength属性上设置的值".我通过在流行的Stackovrflow帖子中找到的webconfig中设置maxJsonLength ="2147483647"来修复了该问题.

Upon debugging in Firebug, I saw the error "The length of the string exceeds the value set on the maxJsonLength property". I fixed it by setting the maxJsonLength="2147483647" in my webconfig as found in a popular Stackovrflow post.

所以我的问题是引起错误的字符串是什么?是整个数据记录的长度,还是返回的一列中数据的长度?

So my question is what was the string that caused the error? Is it the length of the whole data record, or the length of the data in one of the columns returned?

我看过jqGrid返回更多数据的示例.感谢您的见识.

I've seen examples of the jqGrid returning much more data. Thanks for any insight.

更新

我接受了Olegs的建议,并使用Nuget在我的项目中安装了Newtonsoft.Json.然后,我对代码进行了更改以使用它:

I took Olegs advice and used Nuget to install Newtonsoft.Json in my project. I then made changes to my code to use it:

在隐藏代码-.cs中,我有这个代码:

In the codebehind - .cs I have this:

using Newtonsoft.Json;

公共局部类Default2:System.Web.UI.Page {

public partial class Default2 : System.Web.UI.Page {

[WebMethod]
public static string GetDataFromDB()

{
    DataSet ds = new DataSet();


    string con = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCon"].ToString();
    SqlConnection SCon = new SqlConnection(con);
    SCon.Open();
    SqlCommand sqlCmd = new SqlCommand("dbo.usp_GetProjectDetails", SCon);
    sqlCmd.CommandType = CommandType.StoredProcedure;

    sqlCmd.Parameters.Add("@ProjNum", SqlDbType.Int).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@TrakIt", SqlDbType.VarChar, 255).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@Title", SqlDbType.VarChar, 255).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@Status", SqlDbType.VarChar, 255).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@Dept", SqlDbType.Int).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@AssignTo", SqlDbType.Int).Value = DBNull.Value;  //19;
    sqlCmd.Parameters.Add("@RecDate", SqlDbType.DateTime).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@CmpDate", SqlDbType.DateTime).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@ExComp", SqlDbType.Int).Value = DBNull.Value;
    sqlCmd.Parameters.Add("@ExAcReq", SqlDbType.Int).Value = DBNull.Value;

    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    da.Fill(ds);
    SCon.Close();

    return JsonConvert.SerializeObject(ds.Tables[0]);
}

.aspx中的函数如下:

The function in .aspx looks like this:

    <script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            data: "{}",
            url: "Default2.aspx/GetDataFromDB",
            dataType: "json",
            success: function (data) {
                data = data.d;
                $("#list1").jqGrid({
                    datatype: "local",
                        colNames: ["Project #", "Trak-It #", "Priority", "Title", "Status", "Department", "Assigned To", "Resource", "Requestor"],
                        colModel: [
                                          { name: 'Project Number', index: 'Project Number', width: 80, key: true, formatter: 'showlink', formatoptions: { baseLinkUrl: 'Details.aspx', target: '_new' } },
                                          { name: 'Trak-It #', index: 'Trak-It #', width: 80 },
                                          { name: 'Priority', index: 'Priority', width: 80 },
                                          { name: 'Title', index: 'Title', width: 200 },
                                          { name: 'Status', index: 'Status', width: 80 },
                                          { name: 'Department', index: 'Department', width: 180 },
                                          { name: 'Assigned To', index: 'Assigned To', width: 100 },
                                          { name: 'Resource', index: 'Resource', width: 160 },
                                          { name: 'Requestor', index: 'Requestor', width: 140 }
                        ],
                        data: JSON.parse(data),
                        rowNum: 8,
                        rowList: [10, 20, 30],
                        pager: '#pager1',
                        caption: "Test Grid",
                        viewrecords: true,
                        ignoreCase: true,
                        async: true,
                        loadonce: true,
                        gridview: true,
                        width: 1000
                });
            }
        });
    });

</script>

最后在Web.config中,我注释掉了maxjsonLength:

And finally in Web.config, I commented out the maxjsonLength:

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

但是我仍然收到错误=使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了在maxJsonLength属性上设置的值."

But I still get the error = "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

如果我取消注释web.config设置,它就可以正常工作.如果我将其保留为注释状态并带回较少的数据,则它可以正常工作.我想念什么?

If I uncomment the web.config settings, it works just fine. If I leave it commented out and bring back less data, it works fine. What am I missing?

推荐答案

您的意思是System.Web.Script.Serialization.JavaScriptSerializer.MaxJsonLength属性,如果在服务器代码中使用WebServices接口,则需要全局增加该属性.例如,答案中描述了该解决方案.

You mean probably System.Web.Script.Serialization.JavaScriptSerializer.MaxJsonLength property which need be increased globally if you use WebServices interface in the server code. The solution was described in the answer for example.

您询问问题的背景信息.实话说,主要问题是在ASP.NET应用程序中使用非常老的 WebService接口.多年前,这是Microsoft首次尝试根据HTTP请求的Content-Type标头返回XML或JSON数据.它是在.NET Framework 3.5中实现的. Web服务应返回 object (不是字符串),该对象将由.NET框架使用JavaScriptSerializer序列化为JSON字符串.您的代码不直接使用JavaScriptSerializer.因为您不直接使用JavaScriptSerializer,所以只能在web.config中配置JavaScriptSerializer的参数.

You ask about the background information of the problem. To tell the truth the main problem is that usage of very old WebService interface in ASP.NET applications. It was the first attempt by Microsoft, many years ago, to return XML or JSON data based on Content-Type header of HTTP request. It was implemented in .NET Framework 3.5. The web service should return object (not a string) which will be serialized by .NET framework to JSON string by usage of JavaScriptSerializer. Your code don't uses JavaScriptSerializer directly. Because you don't use JavaScriptSerializer directly, you can configure parameters of JavaScriptSerializer only in web.config.

换句话说,如果返回的数据大小可能大于100k左右,则每次必须在web.config中使用JavaScriptSerializerMaxJsonLength设置.

In other words, you have to use MaxJsonLength settings of JavaScriptSerializer in web.config every time if the size of returned data could be larger as about 100k.

Web方法的限制100k在8年前(在2007年)发布.NET Framework 3.5时相对较大.后来,Microsoft引入了WCF接口,该接口使JSON序列化更快,并且没有那么小的限制. WCF现在的情况也太旧了,但是它仍然允许使用性能更高的JSON序列化程序进行手动序列化(请参见

The restriction 100k for web method was relatively large 8 years ago (at 2007) at the time of publishing .NET Framework 3.5. Later Microsoft introduced WCF interface which made JSON serialization more quickly, and have not so small restriction. WCF is of case too old now too, but it allows still to make manual serialization using more performance version of JSON serializer (see the answer for example). After WCF Microsoft introduced ASP.NET MVC and then WebAPI. Now Microsoft works on ASP.NET 5 and MVC version 6, which combine MVC and WebAPI under one name MVC6. Starting with MVC2 (or MVC3) Microsoft stopped to develop own JSON serializer and suggested to use some other one. Microsoft use mostly Newtonsoft.Json (synonyme of Json.NET), which is not the most quick one, but relatively good and powefull.

我不想写太多太普通的东西,但我建议您远离使用WebServices的使用风格,而转到其他一些接口,这使您在选择JSON序列化程序时更具灵活性.如果您支持一些旧代码并且不能使用更多现代技术,那么我建议您使用ASHX句柄,该句柄很旧,但比WebServices灵活得多.建议您查看旧答案,我在其中附加了

I don't want to write too much too common things, but I would recommend you to go away from reto style of usage WebServices and go to some other interface which gives you more flefibility in choosing of JSON serializer. If you support some old code and can't use more modern technologies then I would recommend you to use ASHX handle, which are very old, but much more flexible as WebServices. I recommend you to look in the old answer, where I attached Visual Studio Project which used ASHX handle and return JSON data using Newtonsoft.Json (Json.NET). You can replace Newtonsoft.Json to any other JSON serializer class which you more like.

这篇关于maxJsonLength属性指的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆