2017-05-30 53 views
2

我正在研究AspNetCore中的web應用程序,並試圖通過ajax調用從我的控制器獲取數據。Ajax獲取返回ERR_SPDY_PROTOCOL_ERROR

這裏是我的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); 
     } 
    } 

和我的Ajax調用:

$.get("/pack/GetPackWithAllCards", { packId: pack.packId }, function (pack) { 
     alert(pack); 
}); 

我的錯誤是百達一樣的,我得到 「無法加載資源:淨:: ERR_SPDY_PROTOCOL_ERROR」 與狀態= 0

我的api正確地返回一個包,但我的ajax調用沒有得到它。

+0

遇到此鏈接,可能會有幫助:https://www.techsupportall.com/fix-err_spdy_protocol_error-permanently/和https://zenguard.zendesk.com/hc/en-us/articles/204423722-Chrome -error-ERR-SPDY-PROTOCOL-ERROR- –

+0

它不起作用 – Elykx

回答

1

這是JSON序列化中的錯誤。請參閱文檔https://docs.microsoft.com/en-us/ef/core/querying/related-data#related-data-and-serialization

基於github:https://github.com/aspnet/EntityFrameworkCore/issues/9573。 我發現它只能部分解決我的問題。刪除模型中沒有必要的參考修復了問題。

+0

鏈接繁多的答案不適用於此網站,因爲它們有保證腐爛。所以隨着時間的推移它會變得不那麼有用你能引用相關部分來更清楚地解釋你的答案嗎? – jdv

0

我剛碰到這個錯誤。原因是JSON的序列化錯誤。

檢查您的Packs類和子類是否具有訪問器,這些訪問器可以在未填充所有子類時創建未引發實例異常的對象引用。

例子:

public class SaveStatus 
{ 
    public bool Success 
    { 
    get 
    { 
     return ValidationErrors.Count == 0; 
    } 
    } 
    public List<ValidationError> ValidationErrors { get; set; } 
} 

在這種情況下,如果不填充列表ValidationErrors那麼JSON將無法序列化的成功訪問觸發問題的錯誤。