【问题标题】:WCF internal server error when calling a put method with jQuery使用 jQuery 调用 put 方法时出现 WCF 内部服务器错误
【发布时间】:2011-12-02 19:54:04
【问题描述】:

我正在尝试使用 jQuery 1.7.1 对 WCF webapi 服务执行 put 操作。相关客户端代码为:

$.ajax(
    {
        type: 'PUT',
        contentType: 'application/json',
        dataType: 'json',
        url: '../webapi/esfuerzos/' + id,
        data: { json: args },
        success: function (respuesta) {
            $("cancelarEsfuerzoTerreno").trigger("tap");
        },
        error: function (respuesta) {
            debugger;
        }
    });

以下方法签名在服务器上:

[WebInvoke(UriTemplate = "{idTicket}", Method = "PUT", RequestFormat = WebMessageFormat.Json)]
public HttpResponseMessage Agregar(int idTicket, JsonValue json)

当我调用客户端代码时,我收到 500 - 内部服务器错误响应。这可能是什么原因造成的?

编辑:这是原始的 http 消息

PUT http://localhost/mosaq/sae/webapi/esfuerzos/12 HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://localhost/mosaq/sae/movil/
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost
Content-Length: 355
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=jy0shfatrj4icobvagysrnm2

json%5BpreparacionCoordinacionMinutos%5D=null&json%5BpreparacionCoordinacionHoras%5D=null&json%5BesperaMinutos%5D=null&json%5BesperaHoras%5D=null&json%5BtrasladoIdaFin%5D=null&json%5BtrasladoIdaInicio%5D=null&json%5BtrasladoRegresoFin%5D=null&json%5BtrasladoRegresoInicio%5D=null&json%5BejecucionFin%5D=null&json%5BejecucionInicio%5D=null&json%5Btipo%5D=0

【问题讨论】:

  • 您应该发布与 500 错误相关的异常。这应该告诉我们问题是什么。
  • 如何获得异常?我在方法中放了一个断点,但它没有中断:S
  • Windows 应用程序日志中可能有些内容。
  • 不,没有,已经检查了 =(
  • iis 日志显示:::::1, -, 12/2/2011, 17:26:57, W3SVC1, NICO-VHD, ::1, 217, 840, 1887, 500, 0、PUT、/mosaq/sae/webapi/esfuerzos/12、-、

标签: c# javascript jquery wcf rest


【解决方案1】:

如果您看到服务器端错误,并且像您的服务方法一样从未到达过断点,最好的办法是启用跟踪并使用服务跟踪查看器工具查看输出。可能有很多输出,但重点是错误(红色)和警告(黄色)。

请参阅此question 的答案和 MSDN 文档here 如何配置。

【讨论】:

    【解决方案2】:

    很可能客户端设置了错误的请求类型。例如,应为“text/xml”,但为“appliation/x-www-form-urlencoded”。

    【讨论】:

    • 您能详细说明一下吗?将原始 http 添加到原始帖子
    • 我们观察到 500 次主要是因为错误的请求类型。如果有其他原因,请尝试将合法和错误的请求与 http 调试器进行比较。
    • 内容类型设置为 application/json。 text/json 不能纠正问题。我认为 text/xml 不适用,因为我发送的是 json 而不是 xml。
    • “text/xml”只是一个例子。
    【解决方案3】:

    问题是数据对象没有正确序列化。在 args 对象上调用 json.stringify 解决了这个问题。

    感谢杰森帮我解决了这个问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多