I have a problem with invoking an ASMX web method on a remote server through AJAX in a stand-alone JavaScript file.
I use AJAX to call web methods in two places; in .aspx pages (JavaScript embedded into the page) and in a stand-alone JavaScript file.
The website and web service is hosted on a remote server. The problem is that when the web methods are invoked through the stand-alone JavaScript file, I receive a 500 internal server error with the following error message:
Request format is unrecognized for URL unexpectedly ending in /MyWebMethod
I have found out the following about the problem:
- The error occurs only from the stand-alone JavaScript file. If I run the script embedded in an .aspx page it works fine.
- The error occurs only when invoked remotely. If I run the script from a browser on the server it works fine.
- Adding protocols as suggested in this answer solves the problem, but this is not a feasible solution as I have no control over the Web.config where the solution is deployed.
- I added logging in the web method to see if it was entered when the error occurs. It did not enter the method body.
The AJAX and web methods in question follow this pattern:
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();
}
So to sum up: I have identical code in .js and .aspx files. When visiting the pages remotely, only AJAX invoked from .aspx files works. When run locally, both .js and .aspx works.
Does anyone have any idea what might cause this and how it can be solved? I am perplexed that it works locally or when called through .aspx, but not remotely when called through .js.
EDIT: Here are two HTTP requests:
Locally using Firefox (working)
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
Remotely using Chrome (not working)
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