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

1

11

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?

Stanza answered 17/12, 2012 at 14:24 Comment(3)
Before we go any further, why on earth are you doing this: data: "{'param1': '" + param1 + "','param2': '" + param2+ "' }"? You can declare an actual JavaScript object, you don't have to make it a string that represents the object.Meyerhof
@AnthonyGrist : Thanks for the heads up. Actually I am new to Ajax calls. I will surely update my code to make use of JavaScript Object.Stanza
I'd extend your error callback function to do console.log(errorThrown); then use your browser's developer tools to see what's logged in the console; it might give you a clearer idea of what the error is. Also check that the to_return string represents valid JSON in the request that's failing; jQuery will error if it can't parse the response as JSON when you specify dataType: 'json'.Meyerhof
K
23

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> 
Karmenkarna answered 17/12, 2012 at 14:56 Comment(4)
Thank you @tpeczek. It worked. One quick question: Will it affect performance if I increase the maxJsonLength greater than its default value?Stanza
@Stanza It will not affect the serialization performance (in fact the default for JavaScriptSerializer which you would construct manually is 2097152). The purpose of this limitation is to make developer think if they really want to generate such a big response (after all it is ussually the transfer which takes most of time).Karmenkarna
what is maximum size for the jsonSerializationEngvall
@Engvall as far as I know it is Int32.MaxValue (so about 2147483647)Karmenkarna

© 2022 - 2024 — McMap. All rights reserved.