Return Json from MVC application

I'm trying to return Json data from an MVC Application. I used Fiddler and FireBug to troubleshoot this and the Json data seems to be ok, but for some reason the data still doesn't display. This is my code: public class CalendarController : Controller { public JsonResult ReturnCalendarEvents() { var model = _service.EventList("JackV", "10/1/2011", "10/8/2011"); return Json(model, JsonRequestBehavior.AllowGet); } } Site.Master $.ajax({ type: "POST", async: false, contentType: "application/json; charset=utf-8", url: "/Calendar/ReturnCalendarEvents", data: "{'UserName':'" + UserName + "','starTime':'" + start.format("isoDateTime") + "','endTime':'" + end.format("isoDateTime") + "'}", dataType: "json", success: function (result) { //Logic goes here }, error: function (e) { alert("An Error Occured" + e); } }); So even though in Fiddler I see the actual Json data being returned, in the Site.Master file, this goes straight to the error. The error returned is [Object object]. Not too helpful. I debugged my application and there are no errors anywhere before the data is being returned as Json. Unless the data is not true Json somehow but it sure looks like it. The weird thing is that the ajax function runs, then goes straight to error, then I step into the Visual Studio code for my controller, then it goes back to this ajax function but this time it does error out, it goes to the success code. Somewhere along the way, it stops debugging in FireBug and I'm not sure why the data is not being returned. Can anybody think of anything that might help me figure this out? Thank you. ------------------------------------------------------------------------- Solution: After I looked at everyone's comments i realized that this line was wrong: var schedules = (eval(result.d)); This was part of my Success logic that I didn't include in the post. In FireBug result showed data, but .d was null. This worked just fine in a web application but it doesn't like the .d in the MVC app. I changed the code to this and now it works fine. I still don't know why that function gets hit twice, with the first one erroring out but the 2nd one retrieving the data. I will look into it more. var schedules = (eval(result));

以上就是Return Json from MVC application的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JSON 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏