Reputation: 133
I'm trying to pass a list object from the server to an Ajax success callback, but I'm not receiving data in the console, and the alert
calls in my code don't fire.
I don't have much experience with JS, and I haven't been able to determine why this isn't working. I've already tried changing ActionResult
to JsonResult
, to no avail.
This is the action method:
public ActionResult jason()
{
var list = new CardModel().ItemList;
return Json(list);
}
And this is the Ajax call:
<script>
$(document).ready(function() {
$.ajax({
type: 'GET',
url: "/Card/jason/",
dataType: 'json',
success: function myfunction(data) {
console.log(data);
var list = data;
console.log(list);
$.each(list, function(index, item) {
alert(item);
});
}
});
});
</script>
Upvotes: 0
Views: 72
Reputation: 288
By default, the ASP.NET MVC framework does not allow you to respond to an HTTP GET request you'll need to explicitly allow the behavior by using JsonRequestBehavior.AllowGet like
return Json(list, JsonRequestBehavior.AllowGet);
see this link Why is JsonRequestBehavior needed? for details
Upvotes: 2
Reputation: 883
This only happens when your dataType is not correct. Are you sure you're receiving JSON from the backend?
You should be able to see an error in error
callback function of ajax()
Upvotes: 0