GetJSON Callback Not Firing

I am learning asp.net mvc while working on a test project including SubSonic and jQuery.

The problem I am facing is that every time I want to return something more than a simple string, such as a Json object, I get muted because callbacks don't seem to work or don't return as a failure .

My method for getting a list of tasks in the database:

[AcceptVerbs(HttpVerbs.Get)] public JsonResult GetAllJobs() { var db = new JamesTestDB(); var jobs = from job in db.Jobs select job; return Json(jobs.ToList()); } 

And my javascript to call it:

  function updateJobList() { var url = '<%= Url.Action("GetAllJobs", "Home") %>'; $.getJSON(url, null, function(data, status) { alert("Success!"); }); } 

I played with get, post and getJSON using both built-in and external function definitions for success and failure. Nothing seems to work, but the code definitely makes an Ajax call, just without starting the callback.

+6
json jquery asp.net-mvc
source share
4 answers

Here is the solution!

So it turns out that I did the same for a year:

 public JsonResult SaveData(string userID, string content) { var result = new { Message = "Success!!" }; return Json(result); } 

So, I started doing the same in the new project that I started. Well, the difference? The first was MVC 1.0, and my new MVC 2.0. So what's the difference? You must allow JSON GET requests:

 public JsonResult SaveData(string userID, string content) { var result = new { Message = "Success!!" }; return Json(result, JsonRequestBehavior.AllowGet); } 
+12
source share

jQuery has an error handler that needs to be linked if you want to see errors:

 $("#msg").ajaxError(function(event, request, settings){ $(this).append("<li>Error requesting page " + settings.url + "</li>"); }); 
0
source share

You tried:

$. getJSON (url, function (data, status) {alert ("Success!");});

and also check if the URL resolution is correct:

Alerts (URL)

before calling to check it correctly.

Then check the answer in the Firebug console window in Firefox.

0
source share

The problem is somewhere that I am returning. It seems that pushing anonymous types in Json () seems to make it go bad somehow. By defining a simple class and pushing the values ​​into a new instance, I returned it correctly.

0
source share

All Articles