Py学习  »  Jquery

找不到jQuery Ajax请求,ServiceStack跨域

Community wiki • 2 年前 • 513 次点击  

ServiceStack(9.54),使用JQuery AJAX的POST请求。
跨域请求,提示错误“找不到请求”。

该服务是作为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、FIREFOX、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、Firefox、Opera 12)。

谢谢@mythz,@Jon

和来自stackoverflow问题的@sroes ServiceStack returns 405 on OPTIONS request

步骤1。使用@sroes answer中的代码,我在AppHost中进行了替换。配置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建议的路由和接受Options请求的方法。 我在Routes中添加了,但实际上并没有为Options创建任何函数。

   Routes
      .Add<ReservationRequest>("/TestAPI/CreateReservation", "POST, GET, OPTIONS");

问题解决了,它对我有效,ServiceStack摇滚!!

非常感谢。

更新3: 它也适用于@mythz中提出的代码。

一开始,代码无法编译,因为它找不到EndServiceStackRequest()。

(命名空间ServiceStack.WebHost.Endpoints.Extensions中的扩展方法)。

还需要参考System.Web。httpReq.Method也是httpReq.HttpMethod。

最重要的是选项路线中的声明。

幸运的是,没有必要添加空方法“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 是关于同样的问题。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/157186
 
513 次点击  
文章 [ 1 ]  |  最新文章 2 年前