ServiceStack (9.54)、JQuery AJAX を使用した POST リクエスト。
クロス ドメイン リクエストを送信すると、「リクエストが見つかりません」というエラーが表示されます。
このサービスは、Windows サービスとして自己ホストされます。ファイアウォールはオフです。
私が読んだことは、クロスドメインコール(CORS)の ServiceStack で実行しましたが、結果はありませんでした。
Web ページのコード (localhost で正常に動作) は次のとおりです。
jQuery.support.cors = true;
CallTest()
{
$.ajax({
type: 'POST',
url: 'http://dev.testhost.com:18162/TestAPI',
data: JSON.stringify(myRequest),
contentType: 'application/json',
dataType: 'json',
success: function (response, status) {alert(status); },
error : error: function (xhr, err) { alert(err); }
}
}
//in server the code is
//1. at appHost
SetConfig(new ServiceStack.WebHost.Endpoints.EndpointHostConfig
{
GlobalResponseHeaders =
{ //Signal advanced web browsers what HTTP Methods you accept
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
{ "Access-Control-Allow-Headers", "Content-Type" },
}
}
//2. request
[Route("/TestAPI", "POST")]
[Route("/TestAPI", "GET")]
public class myRequest
{
public string name { get; set; }
public address[] addresses { get; set; }
}
public class myResponse
{
public bool success { get; set; }
public string message { get; set; }
}
// 3. Service
[AddHeader(ContentType = ContentType.Json)]
public myResponse Post(myRequest request)
{
return new myResponse();
}
@mythzの以前の回答、 ServiceStack CORS機能に基づいて、AppHostコードのさまざまな設定でデバッグしました
しかし、それは私にはうまくいきませんでした。
更新 1: IE、FIREFOX、OPERA ではなく、Chrome で動作します
CHROME Request
POST /TestAPI/CreateReservation HTTP/1.1
Connection: keep-alive
Content-Length: 622
Accept: application/json, text/javascript, */*; q=0.01
Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
CHROME RESPONSE
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/3.954 Win32NT/.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
IE10 Request
OPTIONS /TestAPI/CreateReservation HTTP/1.1
Accept: */*
Origin: localhost:28014
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, accept
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host:
Content-Length: 0
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
IE10 Response
HTTP/1.1 404 Not Found
Content-Length: 3
Content-Type: text/plain
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/3.954 Win32NT/.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
更新 2: 問題は解決しました。動作します。テスト済みのすべてのブラウザー (Chrome、IE10、Firefox、Opera 12) に最適です。
ありがとう@mythz、@Jon
およびstackoverflowの質問からの@sroes ServiceStackはOPTIONSリクエストで405を返します
ステップ 1. @sroes の回答のコードを使用して、AppHost.Configure で GlobalResponseHeaders を置き換えました。
Plugins.Add(new CorsFeature());
RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
httpRes.AddHeader("Access-Control-Allow-Origin", "*");
if (httpReq.HttpMethod == "OPTIONS")
{
httpRes.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
httpRes.AddHeader("Access-Control-Allow-Headers",
"X-Requested-With, Content-Type");
httpRes.End();
}
});
ステップ 2: オプション リクエストを受け入れるルートとメソッドの @mythz 提案に基づきます。オプションの関数を実際に作成せずに、ルートに追加しました。
Routes
.Add<ReservationRequest>("/TestAPI/CreateReservation", "POST, GET, OPTIONS");
問題は解決しました、それは私にとってはうまくいきます、ServiceStackは岩です!!
どうもありがとう。
更新 3: @mythz から提案されたコードでも動作します。
EndServiceStackRequest() が見つからなかったため、最初はコードをコンパイルできませんでした。
(名前空間 ServiceStack.WebHost.Endpoints.Extensions の拡張メソッド)。
System.Web への参照も必要です。また、httpReq.Method は httpReq.HttpMethod です。
最も重要なのは、Routes の OPTIONS の宣言です。
幸い、空のメソッド「Options(RequestDto)」を追加する必要はありません。
using System.Web;
using ServiceStack.WebHost.Endpoints.Extensions;
public override void Configure(Container container)
{
Plugins.Add(new CorsFeature());
this.RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.HttpMethod == "OPTIONS")
httpRes.EndServiceStackRequest();
});
Routes
.Add<ReservationRequest>("/TestAPI/CreateReservation", "POST, OPTIONS");
}
また、この質問OPTIONS Verb for Routes with custom CORS headers は同じ問題に関するものです。