gpt4 book ai didi

jquery - 更新到 5.1.0 后将数据 POST 到 WebApi 失败

转载 作者:行者123 更新时间:2023-12-03 22:26:17 29 4
gpt4 key购买 nike

我有一个使用 webapi 加载/保存数据的网络应用程序一切都用来工作正常/今天我发现ASP.NET MVC/webapi有更新,我安装了更新但之后用 jquery POST 数据就不再起作用了。我没有对客户端代码或 webapi Controller 进行任何更改。

我的典型 POSt 语句如下所示:

showProgress(true, self.PanelID(), 'Saving...')
j$.ajax({
type: "POST",
url: uri,
data: dataObj,
error: function (jqXHR, status, error) {
showProgress(false);
GLOBAL.setMessage(self, 'Error saving ' + opType + '<br>' + error, true);
},
success: function (data, status, jqXHR) {
showProgress(false);
if (status === 'success') {
GLOBAL.setMessage(self, opType + " saved ok");
//....

$.ajax 代码出现以下错误

"An error has occurred."ExceptionMessage    "This method or property is not supported after HttpRequest.Form, Files, InputStream, or BinaryRead has been invoked."ExceptionType    "System.Web.HttpException"StackTraceat System.Web.HttpRequest.GetInputStream(Boolean persistEntityBody, Boolean disableMaxRequestLength) at System.Web.HttpRequestWrapper.GetBufferedInputStream() at System.Web.Http.WebHost.SeekableBufferedRequestStream..ctor(HttpRequestBase request) at System.Web.Http.WebHost.HttpControllerHandler.c__DisplayClass11.b__b() at System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.get_StreamContent() at System.Web.Http.WebHost.HttpControllerHandler.LazyStreamContent.CreateContentReadStreamAsync() at System.Net.Http.HttpContent.ReadAsStreamAsync() at System.Net.Http.HttpContentExtensions.d__0`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.ModelBinding.FormatterParameterBinding.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Controllers.HttpActionBinding.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

这些是我的解决方案中的 nuget 包

  package id="Microsoft.AspNet.Mvc" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.Razor" version="3.1.0" targetFramework="net45"   package id="Microsoft.AspNet.SignalR.Client" version="2.0.1" targetFramework="net45"   package id="Microsoft.AspNet.Web.Optimization" version="1.1.2" targetFramework="net45"   package id="Microsoft.AspNet.WebApi" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebApi.Client" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebApi.Core" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebApi.HelpPage.VB" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebApi.OData" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.0" targetFramework="net45"   package id="Microsoft.AspNet.WebPages" version="3.1.0" targetFramework="net45"   package id="Microsoft.Bcl" version="1.1.6" targetFramework="net45"   package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="net45"   package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45"   package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45"   package id="Microsoft.Net.Http" version="2.2.18" targetFramework="net45"   package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" 

有谁知道可能是什么问题吗?知道如何修复它吗?

编辑

示例 Controller 代码:

Public Class ImportController    Inherits ApiController            Public Function SaveImportSession(sessid As string,  entity As SIImportSessionDTO) As HttpResponseMessage        Try            InspectionData.SaveImportSession(sessid, entity)            Dim response = Request.CreateResponse(HttpStatusCode.Accepted)            Return response        Catch ex As HttpResponseException            Throw        Catch ex As Exception            Throw New HttpResponseException(Request.CreateResponse(Of String)(HttpStatusCode.InternalServerError, ex.Message))        End Try    End Function

我仅使用基于属性的路由,这是我的 webapi 路由配置

Public Shared Sub Register(ByVal config As HttpConfiguration)        config.MapHttpAttributeRoutes()        config.Routes.MapHttpRoute( _            name:="DefaultApi", _            routeTemplate:="api/{controller}/{id}", _            defaults:=New With {.id = RouteParameter.Optional} _

问题,我的服务器端代码甚至没有到达,管道上的其他东西抛出异常

谢谢

最佳答案

我发现了问题。

我的邮政代码使用 $.ajax(...),如初始消息所示。

但是 jquery 的 .ajax 将数据发布为 application/x-www-form-urlencoded。这在以前版本的 WebAPI 中有效,但在最新的更新中,它似乎默认不接受 application/x-www-form-urlencoded。

我将内容类型更改为 application/json,并且必须将数据转换为 json,这解决了问题。

所以,而不是

j$.ajax({
type: "POST",
url: uri,
data: dataObj,
success: function(...)

我必须更改为

j$.ajax({
type: "POST",
url: uri,
data: JSON.stringify(dataObj),
contentType: "application/json; charset=utf-8",
success: function(...)

关于jquery - 更新到 5.1.0 后将数据 POST 到 WebApi 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21322464/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com