使用jQuery调用put方法时,WCF内部服务器错误

本文关键字:WCF 内部 服务器 错误 jQuery 调用 put 方法 使用 | 更新日期: 2023-09-27 18:16:35

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

$.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

使用jQuery调用put方法时,WCF内部服务器错误

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

查看这个问题的答案和MSDN文档在这里如何配置

很可能客户端设置了错误的请求类型。例如,应该是"text/xml",而应该是" application/x-www-form-urlencoded"。

问题是数据对象没有被正确序列化。调用json。对args对象进行Stringify解决了这个问题。

感谢Jason帮我解决这个问题!