Sunday, April 3, 2011

What should my JSON string look like in my Action in MVC?

Hi,

in .net mvc my action looks like:

        public ActionResult TestAjax(string testID)
        {

            return Content(@"{first: ""1"", second : ""2""}");
        }

In my javascript I am doing:

function(data) { alert(data.first); }

I am getting [object Object] as the output, why is that?

Is my JSON string wrong?

From stackoverflow
  • You want to do a return with Json not Content

    return Json(new { first = "1", second ="2" });
    
  • How about letting the system deal with it:

        public ActionResult TestAjax(string testID)
        {
            return Json(new {first = 1, second = 2});
        }
    
    mrblah : yeah if I added "applicaton/json" that worked also, thanks!

0 comments:

Post a Comment