2013-07-04 92 views
1

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是大約相同的問題。

+0

看到了這一點? http://stackoverflow.com/questions/13741397/servicestack-cors-feature –

+0

謝謝喬恩,我已經嘗試過了,因爲我寫我的其他評論。 但它沒有工作> – stefan2410

回答

1

查看此前的答案on applying CORS on OPTION HTTP Requests

你可以像一個全球性的過濾器應用CORS頭所有OPTION請求:

Plugins.Add(new CorsFeature()); //Registers global CORS Headers 

this.RequestFilters.Add((httpReq, httpRes, requestDto) => { 
    //Handles Request and closes Responses after emitting global HTTP Headers 
    if (httpReq.Method == "OPTIONS") 
     httpRes.EndServiceStackRequest(); 
}); 
+0

感謝德米斯,但我已經讀過這個問題,我已經試過的代碼。特定的片段,RequestFilters不正確,它無法找到屬性。 我沒有校正的代碼作爲 RequestFilters.Add((httpReq,httpRes,requestDto)=> { 如果(httpReq.HttpMethod == 「選項」) httpRes.End(); }); 沒有結果。 – stefan2410

+0

@ stefan2410可以查看網絡檢查器中的所有HTTP流量,並查看哪些流量失敗。 – mythz

+0

我已經在所有瀏覽器(IE,Firefox,Opera)中測試過它們具有相同的行爲,但除了發送2個請求的Chrome,第一個(未找到404),第二個SUCCESFULL的Chrome之外。 HTTP/1.1 404 Not Found Content-Length:3 Content-Type:text/plain 服務器:Microsoft-HTTPAPI/2.0 X-Powered-By:ServiceStack/3.944 Win32NT/.NET Access-Control-Allow-Origin :* 訪問控制 - 允許 - 方法:GET,POST,PUT,DELETE,選項 訪問控制 - 允許 - 標題:內容類型=====> SECOND OF CHROME => HTTP/1.1 200 OK 傳輸-Encoding:chunked Content-Type:application/json; charset = utf-8 – stefan2410