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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
user1808827
  • 345
  • 2
  • 5
  • 11
  • 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. – Anthony Grist Dec 17 '12 at 14:28
  • @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. – user1808827 Dec 17 '12 at 14:32
  • 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'`. – Anthony Grist Dec 17 '12 at 14:38

1 Answers1

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> 
tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • Thank you @tpeczek. It worked. One quick question: Will it affect performance if I increase the `maxJsonLength` greater than its default value? – user1808827 Dec 18 '12 at 04:58
  • @user1808827 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). – tpeczek Dec 18 '12 at 09:20
  • what is maximum size for the `jsonSerialization` – Jonathan Nov 14 '13 at 05:05
  • 1
    @Jonathan as far as I know it is Int32.MaxValue (so about 2147483647) – tpeczek Nov 14 '13 at 08:17