4

I use the following code to send/receive an object to my mvc controller:

$.ajax({
url: _createOrUpdateTimeRecord,
data: JSON.stringify(data),
type: "POST",
//dataType: "json",
contentType: "application/json; charset=utf-8",
beforeSend: function () {
    $("#loading-overlay").show();
},
success: function (data2) {
    try {   // tried to parse it manually to see if anything changes.
        data2 = JSON.parse(data2);
    }
    catch (err) {

    }
},
error: function (xhr, ajaxOptions, thrownError) {
    alert(thrownError + 'xhr error -- ' + xhr.status);
}

});

On my mvc controller I have my JSON object as string so I don't need the .NET JavascriptSerializer and JsonResult.

My JSON string looks like:

data2 = "{title:'1111111',start:'2014-03-23T16:00:00.000',end:'2014-03-23T18:00:00.000',id:107,hdtid:1,color:'#c732bd',allDay:false,description:''}"

And I always get: "Invalid character"

I already tried to return a string and parse the JSON manually on client side. Therefore I used ContentResult as return type but with no success

    public class JsonStringResult : ContentResult
    {
        public JsonStringResult(string json)
        {
            Content = json;
            ContentType = "application/json";
        }
    }

What is the problem here? The JSON looks fine...

Cheers, Stefan

tereško
  • 58,060
  • 25
  • 98
  • 150
stefan
  • 1,336
  • 3
  • 21
  • 46

2 Answers2

6

Just Try it Json Conrtroller

  public JsonResult fnname()
    {
        string variablename = "{title:'1111111',start:'2014-03-23T16:00:00.000',end:'2014-03-23T18:00:00.000',id:107,hdtid:1,color:'#c732bd',allDay:false,description:''}";
        return Json(variablename , JsonRequestBehavior.AllowGet);
    }

Jquery json passing

 $(document).ready(function() {
   $.post("/controllername/fnname", { }, function (result) {
      alert(result);
   }, "json");
 });
Dinesh
  • 1,005
  • 5
  • 16
  • 38
  • I guess this leads to the problem described here: http://stackoverflow.com/questions/9777731/mvc-how-to-return-a-string-as-json However, I tried the alternavtive way from that answer (deserializing and reserializing) and this works for me... – stefan Mar 20 '14 at 09:56
  • Sorry then i dont know the answer but what is deserializing and reserializing – Dinesh Mar 20 '14 at 09:58
  • It is a mega hack in this case. I have a string and want to return JSON. So I use JavaScriptSerializer.deserialize to get an object from my string and then I can create a JsonResult (Which implicitly uses JavascriptSerializer) to serialize my object again to JSON. I don’t know why but it seems JQuery can read this but I don't understand why it can't read my JSON string…. – stefan Mar 20 '14 at 10:30
  • then y u not do like this just split the string using (':') and make a list this pass and in jquery again we can appending the list as string – Dinesh Mar 20 '14 at 10:37
4

Your data2 is INVALID JSON string. It should be:

data2 = "{\"title\":\"1111111\",\"start\":\"2014-03-23T16:00:00.000\",\"end\":\"2014-03-23T18:00:00.000\",\"id\":107,\"hdtid\":1,\"color\":\"#c732bd\",\"allDay\":false,\"description\":\"\"}"

Read JSON standard here http://json.org

JSON is more strict than plain javascript, key has to be wrapped in double quote, and string has to be wrapped in double quote too, single quote is invalid.

Douglas Crockford designed strict format of JSON for reasons. http://www.yuiblog.com/blog/2009/08/11/video-crockford-json/

His home page has many valuable links too. http://javascript.crockford.com

huocp
  • 3,898
  • 1
  • 17
  • 29