的WebMethod - JSON序列化错误 [英] WebMethod - JSON Serialization Error

查看:174
本文介绍了的WebMethod - JSON序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个jQuery AJAX调用在aspx.cs页后端的WebMethod。我收到的.NET JSON序列化的错误。因此,我在寻找各种方法来解决任何错误或避免使用JSON(为的WebMethods只返回格式):

I am making a jQuery AJAX call to a back-end WebMethod in an aspx.cs page. I am getting an error in the .NET JSON serialization. As such, I am looking for ways to either fix the error or avoid using JSON (the only return format for WebMethods):

使用JSON JavaScriptSerializer序列化和反序列化过程中的错误。字符串的长度超过上maxJsonLength属性设置的值

相关联的堆栈跟踪是: 在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(obj对象,StringBuilder的输出,SerializationFormat serializationFormat)\ r \ n将在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext的背景下,WebServiceMethodData methodData ,IDictionary'2 rawParams)\ r \ n将在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData)

The associated StackTrace is : at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary'2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

后端code是如下(注:结果是,在现实中大概呈现为一个字符串控制的110K):

The backend code is as follows (note: result is, in actuality about 110k of a control rendered to a string):

[WebMethod]
public static string GetContactListControl()
{
    try
    {
        var result = "Hello World!"
        return result;
    }
    catch (Exception e){
        Logging.LogException(e);
        return "Exception Thrown";
    }
}

和我从来没有碰到块,这对我表明,这个问题是我的code之外。

And I am never hitting the catch block, which to me shows that this issue is outside of my code.

我发现涉及改变web.config中插入以下块的修复程序,但它不工作:

I found a fix involving changing the web.config by inserting the following block, but it is not working:

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

Project是.NET 3.5。

Project is .NET 3.5.

感谢您的任何意见和建议!

Thank you for any ideas and suggestions!

推荐答案

在maxJsonLength财产的配置必须设置在web.config中。为了让被IIS允许这种配置,下面&LT; sectionGroup&GT; 必须包含的&LT内部; configSections&GT;

Configuration of the maxJsonLength property must be setup in the web.config. In order to allow this configuration to be permitted by IIS, the following <sectionGroup> must be included inside of <configSections>:

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
  </sectionGroup>
</sectionGroup>

这篇关于的WebMethod - JSON序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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