从JavaScript文件远程调用Web方法时,无法识别请求格式

时间:2015-07-01 11:35:04

标签: c# asp.net ajax web-services asmx

我在独立的JavaScript文件中通过AJAX在远程服务器上调用ASMX Web方法时遇到问题。

我使用AJAX在两个地方调用web方法;在.aspx页面(嵌入到页面中的JavaScript)和独立的JavaScript文件中。

网站和网络服务托管在远程服务器上。问题是当通过独立的JavaScript文件调用Web方法时,我收到500内部服务器错误,并显示以下错误消息:

  

对于意外以/ MyWebMethod

结尾的网址,无法识别请求格式

我已经找到了关于这个问题的以下内容:

  • 错误仅发生在独立的JavaScript文件中。如果我运行嵌入在.aspx页面中的脚本,它可以正常工作。
  • 仅在远程调用时才会发生错误。如果我从服务器上的浏览器运行脚本,它可以正常工作。
  • this answer中的建议添加协议可以解决问题,但这不是一个可行的解决方案,因为我无法控制部署解决方案的Web.config。
  • 我在web方法中添加了日志记录,以查看错误发生时是否输入了。它没有进入方法体。

有问题的AJAX和网络方法遵循以下模式:

function getAjaxResponse() {
    $j.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        cache: false,
        async: true,
        url: "Path/To/Webservice.asmx/MyWebMethod",
        dataType: "json",
        success: function (data) {
            // do stuff
        },

        error: function () {
            // do stuff
        }
    });
}
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public string MyWebMethod()
{
    var obj = SomeMethod();
    return obj.JsonSerialize();
}

总而言之:我在.js和.aspx文件中有相同的代码。远程访问页面时,只有从.aspx文件调用的AJAX才有效。在本地运行时,.js和.aspx都有效。

有谁知道可能导致这种情况的原因以及如何解决这个问题?我感到困惑的是它在本地工作或通过.aspx调用时,但在通过.js调用时不能远程调用。

编辑:以下是两个HTTP请求:

本地使用Firefox(正常工作)

Host: lwepitest75
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: application/json, text/javascript, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://lwepitest75/episerver/Cms/
Cookie: ASP.NET_SessionId=kljsmiofrw21agtcns2fojgn; .EPiServerLogin=13A4F4219A61436A4D33BF118EE4523DE1816A927A39968EDE7EBE1D3DEA8949E39BC7FB047ADE9CE1446FD8134E60CF5A27E0D6BB0098C4FF6D81BF91B134285F0AFAE9C7955C1565EA8C4A2CBE266F722A7950D7E1FB169156DC6D2CD0BF1FEB290AD26F6C935CDD1FE06C9260C5E8A62E5A1897C0C7AD55F1346603C3B5C2; .ASPXROLES=wiPu0FPvbdZVl2g_LZzYK70vb-cg1jl3I6QwspX9dplAfk6Gkp81sqcZ1qrJkGPtfYU-4IYvP5FFLjV8teEiExtRH8jpQryhLCoqAkA6HaezZvHFDO63krXiSUTYalnVYElWivP-yVkhqnHJsxhURDNJQeLNs8sCM5LZKNTGzAjPTxeQjwDO41jeG7OHy9KwKs3lYmvrgNu-RnygBYq0zad0x3x9sNY4UWNBV2j2_421IeP77bgtzyTypdCju_gma_hz-a_OUogwlvpqTwTcgZqfD1mi3zHm0DEolIWiJkhU_6XOzvaCcgSvOZtZ69tIPGQ3VKku0jKEYli_bG7uaHqgoCjcEUqvaIa7IbrSMA3SeTn4jzrCf1z94wl2s5aLQ8eAaUoZTx2alwa6YX7D_O43MEA_6_tU-71C0xooJ0eZWFzmUS8Rek4UcA7tdJvJobp9ZCxHuNg6RmxJ8foS4g2LGO82mjK-s5LBcaAOkpm0GqUgeN0hSdq19pPwVDC0lgV2O8eFy8RVVUQ-h339WFKMnbx20aO0Ahjo8QqF1bYEEysZaMT_xWxnpnQCQibuIq6G5J8_7-IqbmK5I-dd0c6wgURLWDsGjbg4wrp534b9elPJGW44lXrjkFVm4ElQzeNJHQ-kxW-M0qIJrFAHtoSybQ7zlx0giuBGJeFwsgXkIbVJSyVjeaQa2Xk_6ttRg16Z884kWkr1_0MV5gkpEJAkNYop-9RC3IdTS0bsIwQTcoSbD9ZmaF5C_7nSVX4GVroXTnnV0OaM41S6W5KX_pQ_BVxKvKk-1FuGnjoc4GZd0Gg9J1mqsKo5Q4nxSbf5bvsFK6x5K1noNUY3kWw4gg2; __epiXSRF=PfB/aDsSD4U/surUSQ5zCVg+xYzK9P4YOdMckQN9aOg=
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 0

远程使用Chrome(不工作)

Host: lwepitest75
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Accept: application/json, text/javascript, */*
Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://lwepitest75/episerver/Cms/
Cookie: ASP.NET_SessionId=o1e5wycnmicp24fmjnyf0pmo; __epiXSRF=IMktMh1Pgy5shyooquYvJwXU5a7SedjZWBgxmNzwiKw=; __RequestVerificationToken=gWQQtm60CSAvZikKqvFn2OZO9abqMfe8mvUIskLH5jj9MDW35_RQCkUAtsVIxX6lsqw7g8oWSR-eTBQg-mrPXVBRJZFU24HFaP9dTIHmFijPmt-bj4JJ9ofH5wwB0-BDrP1xiMdOb_FvvaVH8-iv6w2; .EPiServerLogin=3A1FC04DC74F4864A9D5E13A515771C94775645BD4616C8A305CBF26DD1BC3EF6704FC3012639F9B945816774B4252C32B38D6A19341341F28D1A436B05F0A3DB117FD053CBE8F30A4E97BE0A3EE06F2A698DC2D1F6B556E3974748B3607D3D6E8319594FDA59EE33EA59BB2F566DEF740D20C18147FFB4C0AFABDD0BDDEC85C; .ASPXROLES=bKm0JfD2Y9zzEdavxDERJltj8arOT9Jqv__2B2FNtD6XRhkJqekrHbQTrk6UBd7PhKixI92jbHL6-ufepok4-FlWEyN3RJ8XMIM7Vsbx1EZAboSz0IDKhZXVIPc_arkaW3Q-SZbSmsqRQhRYoVLE7huW3xUVvJhx26jSmSANgszKWAD4v18B3KupP7Uk68RfhSEnC_IGS01U-4wbfkUMzB2ByzyvUqnRykSKzczr7TdF8gItkGWzIxcy8zSVg8MOBrCyBmR_SwIcsy7vIuAOADENnIJXuZNtJ1-yZ08BPgK867Sm1UJ3d2RWD4v7rrDiI0BraY97MqFK6LuZBD9h_V9eBrMgnbDd4dEnDQwm-ryHsmyxVUswM45LMHya-aZy9wbLFrMo4IU9VqS8T92_2XbzFXkw4lBbYRrguipUwuOMmpAHuuuJIWz8akvbJhF9eQeUEDsK-lHRkEI-oPVfvkRKXXOjOm542hxYXE8fb7BmMFcr27ayU2fCy1Tk3IbdLI17yvdIJhu5rdP9hdQ7ZZR06ErMKgmSW_0-k7BtTIPVfWty6TdORretE2PKSfM8Wd9AAXI9wsvcmj9t0Uahlvql5oiRZ0HYulxNKdIYl9IKTu5V8wPedygDAJSBqYEZnzgf6YFpCQBFIivunuHtRbicwBeSTmKtdblF1wU1ZhPc5tPN_Num-jbvpLONLYeR6zHBfgkgbL-NNTIO-D_wTwy-F1yhK5sITbcPptfwvsz6l1dNNFuriMKz1rtD4n8xsBptGXUQflXzWlzBhjddcE6R7BGGGsB-hhsb-4oNRCFjyOMD3HmdLF7lIFM2G6-9C5V2qCrK7Ob9KcPm-X4vSA2
Proxy-Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 0
Origin: http://lwepitest75

0 个答案:

没有答案