vtortola
vtortola

Reputation: 35915

$.getJSON doesn't call the "successful" function

I have an issue with .getJSON.

I have these two calls, but the first alert is never shown:

<script type="text/javascript" language="javascript">

    $.getJSON('@Url.Action("ChartData")',null,
        function (rdata) {

            alert('data');

        });

        $.post('@Url.Action("ChartData")', null,
        function (rdata) {

            var rdataE = eval(rdata);
            alert(rdata.data);

        });

</script>

I have this controller action:

    public JsonResult ChartData()
    {
        return Json(new { data = "my data" });
    }

I have a breakpoint in the last line, and I can see how it is called twice, but for a reason I don't understand, in the first call the alert is never shown.

I have added the following code at the beginning:

$(document).ajaxError(function (event, request, settings, thrownError) {
    alert('error!');
});

And I can see that there is an error, but I don't know how to check which.

Any idea?

Cheers.

Upvotes: 2

Views: 940

Answers (1)

gram
gram

Reputation: 2782

Try changing your controller action return to this:

return Json(new { data = "my data" }, JsonRequestBehavior.AllowGet);

Upvotes: 6

Related Questions