簡體   English   中英

ASP.NET 發送時核心錯誤 Ajax List.net::ERR_HTTP2_PROTOCOL_ERROR

[英]ASP.NET Core Error When Sending Ajax List net::ERR_HTTP2_PROTOCOL_ERROR

在 asp.net 核心項目中,我想用 ajax 發送一個列表到 controller。當我的列表大小在 10-15 之間時它工作,但是當我嘗試發送超過 99 時,我得到錯誤net::ERR_HTTP2_PROTOCOL_ERROR

看法:

for.....
var items = {
  ProductId: productId,
  ColorId: colorId,
  ShippigId: shippingId,
  Count: colorCount
}
shippingArray.push(items);

var json = JSON.stringify(shippingArray);
var url = '@Url.Action("XXXX","XXXXX")';
$.ajax({
  url: url,
  traditional: true,
  datatype: "JSON",
  async: true,
  data: {
    "body": json
  },
  success: function () {

  }
});       
(99) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}........ ] vendor.bundle.base.js:2 

GET https://localhost:5001/xxxxx/xxxxx?body=%5B%7B%22ProductId%22%3A%2235%22%2C%22ColorId%22%3A%221%22%2C%22ShippigId%22%3A%22324%22%2ne
t::ERR_HTTP2_PROTOCOL_ERROR

Controller:

 public async Task<bool> ShippingFinishDeleteItem(String body)

有人有建議嗎?

在 asp.net 核心項目中,我想用 ajax 向 controller 發送一個列表。當我的列表大小在 10-15 之間時它工作,但是當我嘗試發送超過 99 時,我得到錯誤.net::ERR_HTTP2_PROTOCOL_ERROR

這是什么:

在此處輸入圖像描述

嗯,你的錯誤很明顯。 根據https://www.w3.org/Protocols 約定,我們曾經在以下場景中遇到類似的錯誤

當服務器因為 Request-URI 比服務器願意解釋的長而拒絕為請求提供服務時。

解釋:

根據您的請求,您似乎正在使用GET方法發送請求。 正如您可能知道的那樣,根據瀏覽器配置, GET method最多可以攜帶2,048KB的數據。 超過上述限制的請求將導致net::ERR_HTTP2_PROTOCOL_ERROR錯誤。 您可以在此處獲取更多詳細信息。

解決方案:

之前,任何具體的解決方案我們都應該有一個合乎邏輯的決定,即如果我們對數據的請求長度有可能在那些場景中達到瀏覽器的最大限制,我們最好使用POST Http Verb or Method ,因為POST方法不受限制用於提交名稱/值對的 URL 的大小。 這些對在 header 中傳輸,而不是在 URL 中傳輸。我們甚至可以在 asp.net 內核中嘗試[RequestSizeLimit(500 * 1024 * 1024)] 然而,理想的實現是在大數據請求的情況下使用POST Method 您可以在此處獲取更多詳細信息。

Controller:

        [HttpPost]
        public async Task<bool> ShippingFinishDeleteItem(String body)
        {

            return true;
        }

Javascript/Jquery:

@section scripts {
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {

            var productId = "Pro:";
            var colorId = "Col:";
            var shippingId = "Shi:";
            var colorCount = "Cnt:";
           
            var shippingArray = [];
            for(let i=0; i<=150;i++){
                var items = {
                    ProductId: productId+i,
                    ColorId: colorId+i,
                    ShippigId: shippingId+i,
                    Count: colorCount+i
                }
                shippingArray.push(items);
            }

            console.log(shippingArray);

            var json = JSON.stringify(shippingArray);
            var url = '@Url.Action("ShippingFinishDeleteItem","MyControllerName")';
            $.ajax({
                url: url,
                type: "POST",
                traditional: true,
                datatype: "JSON",
                async: true,
                data: {
                    "body": json
                },
                success: function () {

                }
            });

        });
    </script>
}

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM