jquery – 在更新到5.1.0之后将数据发布到WebApi失败

前端之家收集整理的这篇文章主要介绍了jquery – 在更新到5.1.0之后将数据发布到WebApi失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用webapi加载/保存数据的网络应用程序
所有以前工作好/
今天我发现有一个更新到ASP.NET MVC / webapi,我安装了更新
但是之后,使用 jquery发送数据不再有效.
我在客户端代码或webapi控制器中没有做任何改变.

我的典型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,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"

StackTrace
at 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" 

有人知道有什么问题吗?任何想法如何解决

编辑

样品控制器代码




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 post数据应用程序/ x-www-form-urlencoded.
这在以前的WebAPI版本中起作用,但是在最新的更新中,它似乎默认不接受application / x-www-form-urlencoded.

我将内容类型更改为application / json,我不得不将数据转换为json,这样可以解决问题.

所以,而不是

j$.ajax({
    type: "POST",success: function(...)

我不得不改变

j$.ajax({
    type: "POST",data: JSON.stringify(dataObj),contentType: "application/json; charset=utf-8",success: function(...)
原文链接:https://www.f2er.com/jquery/176110.html

猜你在找的jQuery相关文章