【问题标题】:Error when sending JSON to Web API if too long如果太长,将 JSON 发送到 Web API 时出错
【发布时间】:2021-07-08 14:39:25
【问题描述】:

我在将 JSON 对象发送到 .NET Web API 时遇到问题。我有多个数组,我将其转换为 JSON 并发送到 Web 服务。该对象的总长度刚刚超过 50,000 个字符。如果我对数组进行切片,一切都很好。只要少于 2500 个字符,我发送的 2 个项目都没有关系。

   let value = JSON.stringify(this.excelData.results.slice(2, 2));
   axios({
   method: 'post',      
   url: 'http://adminapiSystem.com/api/saveJSONData',
   params: {          
     value: value
    }    
   })
  .then((res) => {
    console.log('Saved')
  }) 
  .catch(err => {
      console.log(err)
  
  } )

我的印象是最大长度是 2097152 或 4 兆

我的网络服务是

     Public Function PostValue(ByVal value As String) As String
        Dim ret As String = SendDataSQL(value)
        Return ret
    End Function

错误是

        xhr.js?b50d:177 POST http://xxxxx/api/saveJSONData?value=    [%7B%22Status%22:%22Active%22,%22DBID%22:%22SB%22,%22ID%22:11691,%22Search%22:null,%22Year_Start%22:2015,%22Year_End%22:3000,%22Enrolled%22:null,%22WithDrawn%22:null,%22Last_Name%22:%22Baer%22,%22First_Name%22:%22Lea%22,%22MI%22:null,%22Suffix%22:null,%22PubMed_ForeName%22:null,%22PubMed_Initials%22:null,%22Email%22:%22Lea.Baer%40stonybrookmedicine.edu%22,%22ORCID%22:null,%22Membership_Type%22:%22Affiliate%22,%22ProgramName%22:%22Non-Programmatically+aligned%22,%22Program_Code%22:%22ZY%22,%22Program_Roles%22:null,%22Search_Term%22:null,%22PMID_New%22:null,%22Harvard_Search_PMIDs%22:null,%22PMID_Current%22:null,%22PMID_Exclude%22:null,%22Affiliations%22:%22Stony+Brook%22,%22Authoring_Names%22:null,%22Basic_Search_String%22:null,%22Send_Email%22:null,%22Custom_Search_String%22:null,%22Academic_Title%22:null,%22Department%22:%22Medicine%22%7D,%7B%22Status%22:%22Active%22,%22DBID%22:%22SB++++++++%22,%22ID%22:11683,%22Search%22:null,%22Year_Start%22:2015,%22Year_End%22:3000,%22Enrolled%22:null,%22WithDrawn%22:null,%22Last_Name%22:%22Bahou%22,%22First_Name%22:%22Wadie%22,%22MI%22:%22F%22,%22Suffix%22:null,%22PubMed_ForeName%22:null,%22PubMed_Initials%22:null,%22Email%22:%22Wadie.bahou%40stonybrookmedicine.edu%22,%22ORCID%22:null,%22Membership_Type%22:%22Affiliate%22,%22ProgramName%22:%22Non-Programmatically+aligned%22,%22Program_Code%22:%22ZY%22,%22Program_Roles%22:null,%22Search_Term%22:null,%22PMID_New%22:null,%22Harvard_Search_PMIDs%22:null,%22PMID_Current%22:null,%22PMID_Exclude%22:null,%22Affiliations%22:%22Stony+Brook%22,%22Authoring_Names%22:null,%22Basic_Search_String%22:null,%22Send_Email%22:null,%22Custom_Search_String%22:null,%22Academic_Title%22:null,%22Department%22:%22Medicine%22%7D,%7B%22Status%22:%22Active%22,%22DBID%22:%22SB++++++++%22,%22ID%22:11685,%22Search%22:null,%22Year_Start%22:2015,%22Year_End%22:3000,%22Enrolled%22:null,%22WithDrawn%22:null,%22Last_Name%22:%22Bakoulis%22,%22First_Name%22:%22Anastasia%22,%22MI%22:null,%22Suffix%22:null,%22PubMed_ForeName%22:null,%22PubMed_Initials%22:null,%22Email%22:%22Anastasia.Bakoulis%40stonybrookmedicine.edu%22,%22ORCID%22:null,%22Membership_Type%22:%22Affiliate%22,%22ProgramName%22:%22Non-Programmatically+aligned%22,%22Program_Code%22:%22ZY%22,%22Program_Roles%22:null,%22Search_Term%22:null,%22PMID_New%22:null,%22Harvard_Search_PMIDs%22:null,%22PMID_Current%22:null,%22PMID_Exclude%22:null,%22Affiliations%22:%22Stony+Brook%22,%22Authoring_Names%22:null,%22Basic_Search_String%22:null,%22Send_Email%22:null,%22Custom_Search_String%22:null,%22Academic_Title%22:null,%22Department%22:%22Surgery%22%7D] 404 (Not Found)
dispatchXhrRequest @ xhr.js?b50d:177
xhrAdapter @ xhr.js?b50d:13
dispatchRequest @ dispatchRequest.js?5270:52
Promise.then (async)
request @ Axios.js?0a06:61
wrap @ bind.js?1d2b:9
saveAllData @ publication_report.vue?02c8:145
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1854
invoker @ vue.runtime.esm.js?2b0e:2179
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1854
Vue.$emit @ vue.runtime.esm.js?2b0e:3888
click @ QBtn.js?9c40:101
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1854
invoker @ vue.runtime.esm.js?2b0e:2179
original._wrapper @ vue.runtime.esm.js?2b0e:6917
publication_report.vue?02c8:156 Error: Request failed with status code 404
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:17)
    at XMLHttpRequest.handleLoad (xhr.js?b50d:62)

我的网络配置有

       <scripting>
  <webServices>
    <jsonSerialization maxJsonLength="2147483644" />
  </webServices>
     </scripting>

和

  <system.web>
  <compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" maxUrlLength="10240" relaxedUrlToFileSystemMapping="true" />
<customErrors mode="Off" />
 </system.web>

最后一项——我使用 HostBuddy 作为我的托管服务

【问题讨论】:

    标签: javascript json web asp.net-web-api


    【解决方案1】:

    您正在发送 POST 请求,并且数据在 URL 中。

    GET 最大长度为 2,048 个字符,您将无法发送更多。这是一个 HTTP 限制。

    您可以做的是通过BODY中的POST方法发送这些数据,没有限制。

    这个 Axios 代码应该适合你:

    axios({
      method: 'post',
      url: 'http://adminapiSystem.com/api/saveJSONData',
      data: {
        value: value
      }
    });
    

    【讨论】:

    • 谢谢——我没看到。我想当它说 POST 时需要将参数更改为数据,现在得到一个 POST 的 CORS 错误。但这是一个不同的问题,应该更容易通过托管公司解决
    猜你喜欢
    • 2014-04-18
    • 2017-04-07
    • 1970-01-01
    • 2012-12-01
    • 2012-02-24
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多