I am facing a strange issue which happens in some cases but not in regular scenario. I have an ajax call which sends some data to my controller. In majority cases, the ajax call posts data to controller correctly but the case where it fails (exception) I get a log that the json received is incomplete. I don't really know what is the root cause of this erroneous behaviour. Any pointers would be of great help...
Here is my code -
C#
public class MyController : System.Web.Mvc.Controller {
....
....
public JsonResult doPost(string x, string y, int z) {
private JavaScriptSerializer _serialiser = new JavaScriptSerializer();
List<Dictionary<string, object>> items = _serialiser.Deserialize<List<Dictionary<string, object>>>(x);
// deserialsation throws argument exception
...
// do stuff after that...
}
...
}
Exception details
type - System.ArgumentException
message - Unterminated string passed in.
stack trace -
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
Javascript
// A - is a big object
var jsonData = { x: JSON.stringify(A),
y: new Date().toString(),
z: A.Index
};
$.ajax ({
url : '/my/doPost',
type: 'POST', contentType: 'application/json; charset=utf-8;',
cache: false,
data: JSON.stringify(jsonData),
dataType: 'json',
async: true,
success: function () {
// do something on success
},
error: function (jqXhr, textStatus, errorThrown) {
// do something on error
}
});
Thanks Saarthak