Return large string from ajax call using Jquery to Web Method of ASP.NET

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 07:49:44

问题


Inside my ASP.NET website I am calling a Web Method using Jquery as follows:

 $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: "{'param1': '" + param1 + "','param2': '" + param2+ "' }",
    dataType: 'json',
    url: "Default.aspx/TestMethod",       
    error: function (jqXHR, textStatus, errorThrown) {
        alert("error: " + textStatus);                     
    },
    success: function (msg) {
        document.getElementById("content").innerHTML = msg.d;
    }
});  

Web Method Definition is:

[System.Web.Services.WebMethod]
public static String TestMethod(String param1, String param2)
{       
     String to_return = /* result of operations on param1 and param2 */;        
     return to_return;
}

My result is a String containing HTML code.
It is working perfect if the to_return string is small.
But it is giving me error as:

500 Internal Server Error 6.22s

I tried to explore it using FireBug in Response it shows me:

{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}

Using breakpoints in Visual Studio, I have copied the to_return string into a text file. Size of the file became: 127 KB.
What possibly went wrong?


回答1:


Most probably you have exceeded MaxJsonLength, by default it is 102400 characters and your string is bigger. You can increase this limit in web.config:

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


来源:https://stackoverflow.com/questions/13915953/return-large-string-from-ajax-call-using-jquery-to-web-method-of-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!