IIS のローカル インスタンスで ASP.NET Web API アプリケーションを実行しています。Web アプリケーションは CORS で構成されています。私が呼び出している Web API メソッドは次のようになります。
[POST("/API/{foo}/{bar}")]
public String DoSomething(String foo, String bar)
{
return "Hello, World!";
}
クライアント側では、次のリクエストを実行しています:
$.ajax({
url: "http://machine/API/Foo/Bar",
type: "POST",
success: function (data) {
console.log("Success: " + data);
},
error: function (xhr, status, message) {
console.log("Failure: " + status + ", " + message);
}
});
Firefox 15.0.1 内の対応するコンソール ログは次のようになります。
test.html (line 38)
POST http://machine/API/Foo/Bar
200 OK
112ms
jquery-1.7.2.js (line 8240)
Success: "Hello, World!"
これは、ローカル IIS サーバーにアクセスしている場合と、Visual Studio 内の IIS Express 開発インスタンスの対応する URL にアクセスしている場合に問題なく動作します。
単一の要求ヘッダーを追加すると、ローカル IIS サーバーに対して次の要求が失敗します。
$.ajax({
url: "http://machine/API/Foo/Bar",
type: "POST",
onBeforeSend: function (xhr, settings) {
xhr.setRequestHeader("A", "B");
},
success: function (data) {
console.log("Success: " + data);
},
error: function (xhr, status, message) {
console.log("Failure: " + status + ", " + message);
}
});
私が見る非常に詳細なログメッセージは次のとおりです。
Failure: error,
Visual Studio 内の IIS Express 開発インスタンスに対して行うと、同じPOST
要求が成功します。
Fiddler では、完全な IIS サーバーまたは Visual Studio 内の IIS Express を対象としているかどうかに関係なく、前の 2 つの要求は失敗することなく通過します。
// Request
POST http://machine/Foo/Bar HTTP/1.1
Host: machine
Content-Length: 0
// Response
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 38
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 09 Oct 2012 20:16:29 GMT
"Hello, World!"
順列{With Headers, Without Headers} x {Full IIS, IIS Express}
はすべて、Fiddler 内で同一の出力を生成します。
要するに、要求ヘッダーを設定すると、ローカルの完全な IIS インスタンスに対して AJAX 呼び出しが失敗するのはなぜですか?
さらに、Fiddler ではリクエストが成功するのに、Javascript コードでは成功しないのはなぜですか? 適切な IIS インスタンスで要求ヘッダーを設定できない場合、これは深刻な問題です。