views:

706

answers:

2

I'm using asp.net page that is fully ajaxified (with jquery lib) and calling another asp.net callback page to get/post data to server. Some users of my page experiencing following error when serializing json object

there was an error deserializing the object of the type ... object type ... contains invalid utf8 bytes

   $.ajax({
    type: "POST",
    async: false,
    url: 'AjaxCallbacks.aspx?Action=' + actionCode,
    data: {
       objectToSerialize: JSON.stringify(obj, null, 2)
    },
    dataType: "json",
    success: function(operationResult) {
       //handle success
    },
    error: function(xhttp, textStatus, errorThrown) {
       //handle error
    }
 });

to deal with this I've added "contentType" option ...

 $.ajax({
    type: "POST",
    async: false,
    url: 'AjaxCallbacks.aspx?Action=' + actionCode,
    data: {
       objectToSerialize: JSON.stringify(obj, null, 2)
    },
    contentType: 'application/json; charset=utf-8', //<-- added to deal with deserializing error
    dataType: "json",
    success: function(operationResult) {
       //handle success
    },
    error: function(xhttp, textStatus, errorThrown) {
       //handle error
    }
 });

but now I can not read this object on server side as I could before:

string objectJson = Request.Params["objectToSerialize"].ToString();

I got following error: "Object reference not set to an instance of an object."

Any ideas?

+1  A: 

The reason you get NullReferenceException in the second case is because you are using application/json as a Content-Type header when sending your request and on the server side ASP.NET expects data to arrive as a form posting when it populates the Request object and so it doesn't contain the objectToSerialize parameter. You can try with the following instead:

contentType: 'application/x-www-form-urlencoded; charset=utf-8'

Or stick with application/json and read and parse the request stream manually:

using (var reader = new StreamReader(Request.InputStream))
{
    var input = reader.ReadToEnd();
    var objectToSerialize = input.Split('=')[1];
}
Darin Dimitrov
A: 

Here is how I solved my problem: Since I change contenttype server thinks that data should come in form collection, instead it comes from request.inputstream, so I wrote function to read & decode inputstream and the rest of the code is not changed:

  /// <summary>
  /// reads request input stream and decodes it so it can be deserialized to .net object
  /// </summary>
  /// <returns>decoded request input stream</returns>
  private string GetInputStream()
  {
     string inputContent;
     using (var sr = new System.IO.StreamReader(Request.InputStream))
        inputContent = sr.ReadToEnd();

     return Server.UrlDecode(inputContent);
  }

So far this works.

Just realized that there was an answer similar to mine (2 point) but missing decoding without which serialization to .net object would not work. Also I hade to strip GetInputStream() given string from "jsonObjectName=" to succedd with serialization to .net object, simmilar to what was suggested by spliting stream string

krul