问题
Following on from my original question: Not receiving JSONP callback
I am now trying to process the information in the JSONP callback and I cannot seem to get any data to "load" i.e. the rsp
object appears to be empty/null.
VenuOffersDAO (UPDATE: Not using this to simplify things)
This is in a separate class library project outside of the MVC4 project. So, my controller
does not use a model
.
public static List<Offer> GetVenuesOffers(int venueId)
{
using (var ctx = new MyDbEntities())
{
var venue = (...removed for brevity...).FirstOrDefault();
return
venue != null
? GetVenuesOffers(venue)
: null;
}
}
public static List<Offer> GetVenuesOffers(Venue venue)
{
using (var ctx = new MyDbEntities())
{
...removed for brevity...
return offers.ToList();
}
}
Controller (UPDATE: Now using a more simple object instead of the generated EF one.)
This is within a ASP.NET MVC4 project.
public class VenueOffersController : Controller
{
[JsonpFilter]
public JsonResult GetOffersForVenue(int venueId)
{
var offers = new List<VenueOffers>();
using (var ctx = new BoonEntities())
{
offers = (from o in ctx.Offers
where o.VenueID == venueId
select new VenueOffers
{
Id = o.ID,
VenueId = venueId,
Title = o.Title
}).ToList();
}
return Json(offers.ToList(), JsonRequestBehavior.AllowGet);
}
}
[Serializable]
public class VenueOffers
{
public int Id { get; set; }
public int VenueId { get; set; }
public string Title { get; set; }
}
HTML Page
This is within a ASP.NET v4 Web-Forms Project as a plain HTML file.
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
var url = "http://localhost/MySite.ContentDelivery/VenueOffers/GetOffersForVenue/?";
function getOffers() {
// build the URL
debugger;
var call = url + "venueId=48&callback=?";
// make the ajax call
$.getJSON(call, function (rsp) {
alert(rsp.offers); // 'undefined'
alert(rsp); // empty
var html = "";
$.each(rsp.offers.offer, function () {
var offer = this;
html += "<span" + offer.Title + "</span> <br />";
});
$("#offersDiv").append(html);
});
}
// get the offers
$(document).ready(function () {
alert('go..');
$(getOffers);
});
</script>
<div id="offersDiv"></div>
</body>
回答1:
Your controller action is returning an array (List<Offer>
). Your JSON result probably looks something like this:
[{"Name":"some name 1"}, {"Name":"some name 2"}, ...]
So inside your success callback you could loop through the results:
$.getJSON(call, function (rsp) {
var html = "";
$.each(rsp, function () {
var offer = this;
html += "<span>" + offer.Name + "</span><br />";
});
$("#offersDiv").append(html);
});
Also put a breakpoint inside your controller action and make sure that the offers
variable that you are returning actually contains some elements.
Also notice that you had a missing closing >
for the span
element you were generating dynamically.
Another problem I can see with your code is that you could replace your document.ready handler with:
$(getOffers);
来源:https://stackoverflow.com/questions/15054200/jsonp-callback-response-data-is-null