I'm working on a webapp in AspNetCore and i'm trying to get data from my controller by an ajax call.
Here is my Api:
[HttpGet]
public async Task<IActionResult> GetPackWithAllCards(int? packId)
{
if (packId == null)
{
return Json("An error has occured");
}
else
{
var pack = await _context.Packs
.Include(p => p.TagPacks)
.ThenInclude(tp => tp.Tag)
.Include(p => p.CardPacks)
.ThenInclude(cp => cp.Card)
.ThenInclude(c => c.FaceCards)
.ThenInclude(fc => fc.Face)
.ThenInclude(fc => fc.Template)
.Include(p => p.CardPacks)
.ThenInclude(cp => cp.Card.FaceCards)
.ThenInclude(fc => fc.Face.Image)
.Include(p => p.CardPacks)
.ThenInclude(cp => cp.Card.FaceCards)
.ThenInclude(fc => fc.Face.Sound)
.SingleOrDefaultAsync(m => m.PackId == packId);
if (pack == null)
{
return Json("An error has occured");
}
return Ok(pack);
}
}
and my ajax call:
$.get("/pack/GetPackWithAllCards", { packId: pack.packId }, function (pack) {
alert(pack);
});
My error is allways the same, i get "Failed to load resource: net::ERR_SPDY_PROTOCOL_ERROR" with status = 0
My api return correctly a pack but my ajax call don't get it.
This is error in JSON serialization. Please refer to docs here https://docs.microsoft.com/en-us/ef/core/querying/related-data#related-data-and-serialization
based on github:https://github.com/aspnet/EntityFrameworkCore/issues/9573. I found it only partially solving my problem. Removing not necessary reference in model fixed the issue.
I just ran into this error. The reason was a serialization error to JSON.
Check if your Packs class and child classes have accessors which could create an Object reference not set to an instance exception if all child classes are not populated.
Example:
public class SaveStatus
{
public bool Success
{
get
{
return ValidationErrors.Count == 0;
}
}
public List<ValidationError> ValidationErrors { get; set; }
}
In this case if the list ValidationErrors is not populated then JSON will not be able to serialize the Success accessor triggering the error in question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With