DougJones
DougJones

Reputation: 782

JsonResult getting truncated SOME of the time

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

Upvotes: 2

Views: 3698

Answers (2)

DougJones
DougJones

Reputation: 782

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.

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56429

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> 

Upvotes: 1

Related Questions