问题描述
ServiceStack (9.54),使用 JQuery AJAX POST 请求.
跨域请求,提示Request not found"错误.
ServiceStack (9.54), POST request with JQuery AJAX.
cross domain request, prompts an Error "Request not found".
该服务作为 Windows 服务自托管.防火墙已关闭.
the service is self-hosted as windows service. FireWall is off.
我已经在 ServiceStack 中为跨域调用 (CORS) 完成了我阅读的内容,但没有结果.
I have done, what I read, in ServiceStack for cross domain calls (CORS), but without result.
在网页中,代码(在本地主机上成功运行)如下.
In the web page, the code (which works successfully on localhost) is the following.
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 功能
I debugged with different settings in AppHost code, based on @mythz previous answers, ServiceStack CORS Feature
但它对我不起作用.
更新 1:适用于 Chrome,不适用于 IE、FIREFOX、OPERA
Update 1: It works for Chrome, not for 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).
Update 2: Problem solved, it works, perfect for all browsers tested (Chrome,IE10, Firefox,Opera 12).
谢谢@mythz,@Jon
Thank you @mythz, @Jon
和来自 stackoverflow 问题的 @sroesServiceStack 在 OPTIONS 请求时返回 405
and @sroes from the stackoverflow question ServiceStack returns 405 on OPTIONS request
步骤 1. 使用@sroes 答案中的代码,我在 AppHost.Configure 中替换了 GlobalResponseHeaders,
step 1. using code from @sroes answer, I replaced in AppHost.Configure the 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 中添加了,实际上并没有为 Options 创建任何函数.
step 2: based on @mythz proposal for a route and a method that accepts Options requests. I added in Routes, without actually to create any function for Options.
Routes
.Add<ReservationRequest>("/TestAPI/CreateReservation", "POST, GET, OPTIONS");
问题解决了,对我有用,ServiceStack 太棒了!!
Problem solved, it works for me, ServiceStack rocks !!
非常感谢.
更新3:它也适用于来自@mythz 的建议代码.
Update 3 : It works also with the proposed code from @mythz.
一开始,代码无法编译,因为找不到EndServiceStackRequest().
At the beginning, the code could not compile, because it could not find the EndServiceStackRequest().
(ServiceStack.WebHost.Endpoints.Extensions 命名空间中的扩展方法).
(an extension method in the namespace ServiceStack.WebHost.Endpoints.Extensions).
还需要对 System.Web 的引用.httpReq.Method 也是 httpReq.HttpMethod.
A reference to System.Web also required. Also the httpReq.Method is httpReq.HttpMethod.
最重要的是 Routes for OPTIONS 中的声明.
The most important is the declaration in Routes for OPTIONS.
幸运的是,没有必要添加空方法'Options(RequestDto)`.
Fortunately, it is not necessary to add an empty method '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 路由的动词自定义 CORS 标头 是关于相同的问题.
Also, this question OPTIONS Verb for Routes with custom CORS headers is about the same issue.
推荐答案
请参阅此较早的回答 关于在 OPTION HTTP 请求上应用 CORS一>.
See this earlier answer on applying CORS on OPTION HTTP Requests.
您可以使用全局过滤器将 CORS 标头应用于所有 OPTION 请求,例如:
You can apply the CORS headers to all OPTION requests with a global filter like:
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();
});
这篇关于未找到 jQuery Ajax 请求,ServiceStack 跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!