2

I am using MVC 3 and have an action that returns a JsonResult with 187 name value pairs (as a List<OrientationData>), but often the data received from the ajax call is truncated and cannot be parsed.

This always returns the same 187 items via the JsonResult, so if it was a length issue, I'd think it would fail EVERY time. Here's the action:

[HttpPost]
    public JsonResult GetAllMetrics()
    {
        var metrics = metric.GetAllMetrics();
        return Json(metrics);
    }

This is the jQuery ajax call:

$.ajax({
            url: urlGetAllMetrics,
            type: 'POST',
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (ajaxData) {
                if (ajaxData && ajaxData.length > 0) {
                    //populate data here
                }
            },
            error: function (msg) {
                alert(msg);
            }
        });

The results returned to the ajax call are sometimes cut off and appear to actually cut off at 2 different places. What are some possible reasons for this?

I'd also like to note that when I use Fiddler to capture traffic, it works EVERY time without truncating the returned data (I have no idea why just yet). When I don't use Fiddler, I often receive an error in the ajax due to it being unable to parse the string to json. The data is an array with Value and Text string properties. The text being returned just cuts off:

...,{"Value":"h12","Text":"h12 name goes here"},{"Val
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
DougJones
  • 774
  • 1
  • 9
  • 26

2 Answers2

1

It will be truncating because of the default value (102400 - 100kb) of the maxJsonLength property. Try changing it in your web.config:

<configuration> 
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="50000000"/>
            </webServices>
        </scripting>
    </system.web.extensions>
</configuration> 
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Thanks for the suggestion, but that didn't work for me. The text is still getting cut off. Since it does work some of the time, I assumed it wasn't a length issue. I'm sure stranger things have happened though. – DougJones Mar 05 '13 at 16:28
  • It is? Then I would bet that it's the limit set on the `JavaScriptSerializer` used internally. Try implementing the code in this answer http://stackoverflow.com/a/7207539/1039608 – Mathew Thompson Mar 05 '13 at 16:37
0

I could not figure out the solution to this problem, so...

I added in a WCF service using the webHttpBinding as shown in the answer to WCF: maxStringContentLength always set to 8192 I set the following attributes on the service class:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

and made sure to add the DataContract and DataMember attributes to the underlying class:

[DataContract]
public class OrientationData
{
    [DataMember]
    public string Value { get; set; }
    [DataMember]
    public string Text { get; set; }
}

I also switched the method (now with an OperationContract on the interface) to this:

public List<OrientationData> GetAllMetrics()
    {
        var metrics = metric.GetAllMetrics();
        return metrics;
    }

I wish I had a better answer, but if anyone else runs across this problem, this is 1 way to get through it. Of course, I'd bet switching to MVC 4 would fix it too via this answer as mentioned by David Murdoch on another post.

Community
  • 1
  • 1
DougJones
  • 774
  • 1
  • 9
  • 26