ServiceStack(9.54),帶JQuery AJAX的POST請求。
跨域請求,提示錯誤「請求未找到」。未找到jQuery Ajax請求,ServiceStack跨域
該服務是作爲Windows服務自行託管的。防火牆關閉。
我已經在ServiceStack中完成了跨域調用(CORS),但沒有結果。
在網頁中,代碼(在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();
}
我調試與APPHOST代碼不同的設置,基於@mythz以前的答案,ServiceStack CORS Feature
但它並沒有爲我工作。
更新1:它適用於Chrome瀏覽器,而不是IE,火狐,OPERA
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,火狐,歌劇12) 。
使用代碼謝謝@mythz,@喬恩
和@sroes從計算器問題 ServiceStack returns 405 on OPTIONS request
①第一階段@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。
最重要的是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是大約相同的問題。
看到了這一點? http://stackoverflow.com/questions/13741397/servicestack-cors-feature –
謝謝喬恩,我已經嘗試過了,因爲我寫我的其他評論。 但它沒有工作> – stefan2410