Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getJSON Callback Not Firing

I'm learning asp.net mvc by working on a test project including SubSonic and jQuery.

The problem that I'm encountering is that every time I want to return something more than a simple string, like a Json object, I get stymied because callbacks don't seem to fire, or come back as failed.

My method to get a list of jobs 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've played around with get, post and getJSON using both inline and outside function definitions for success and failure. Nothing seems to work, but the code is definitely making the Ajax call, just not firing the callback.

like image 419
James Avatar asked Sep 23 '09 22:09

James


1 Answers

Here is the solution!!

So it turns out I had been doing the the exact same way for over a year:

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

    return Json(result);
}

So, I started to do it the same way on a new project I started up. Well, the difference? The first one was MVC 1.0, and my new one is MVC 2.0. So what's the difference? You have to allow JSON GET requests:

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

    return Json(result, JsonRequestBehavior.AllowGet);
}
like image 118
Nexxas Avatar answered Oct 29 '22 22:10

Nexxas