Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 984
user1808827 Avatar asked Dec 17 '12 14:12

user1808827


1 Answers

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> 
like image 108
tpeczek Avatar answered Oct 05 '22 00:10

tpeczek