Dejan.S
Dejan.S

Reputation: 19128

Differences using JSON with MVC & MVC2?

I recently started working with MVC or MVC2 to be more exact. I found a tutorial yesterday that was using JSON to populate a dropdowlist. Im not sure why this did not work with a MVC2 project and only with a MVC. Anybody got the time to just peep this site and maybe see what it might be? http://www.dotnetcurry.com/ShowArticle.aspx?ID=466. It's that JSON example, its homecontroler and the view code only

I really want to know why

thanks

Upvotes: 1

Views: 1649

Answers (1)

Nicholas Murray
Nicholas Murray

Reputation: 13533

There has been a change to JsonResult in MVC 2 and therefore it will no longer work with HTTP GET to avoid JSON hijacking.

So you could either change your code to return via HTTP POST or allow GET behaviour which may leave you open to JSON hijacking.

Try modifying your code to follow the to the format if you wish to use GET

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetListViaJson()
{
  return Json(GenerateNumbers(), JsonRequestBehavior.AllowGet);
}

Or use the recommended POST

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult GetListViaJson()
{
  return Json(GenerateNumbers());
}

Upvotes: 6

Related Questions