我正在本地机器上测试一些 Web 服务。因为测试页面位于端口 80 的根目录上,并且 Web 服务位于不同的端口上,所以我从 Chrome 的诊断工具中收到以下错误:
XMLHttpRequest cannot load http://PCNAME:8083/PackageSearch. Origin http://PCNAME is not allowed by Access-Control-Allow-Origin.
经过一番搜索,我发现了 ServiceStack 的 CORS 特性,并将以下属性放在我的 Web 服务上:
[EnableCors(allowedMethods: "GET, POST")]
但是,错误仍然存在。这是ajax调用:
function packageSearch(tourCode) {
var searchUrl = url + 'PackageSearch';
var searchData = {
TourCode: tourCode,
};
$.ajax(searchUrl,{
data : JSON.stringify(searchData),
type: 'POST',
contentType: 'application/json, charset=utf-8',
dataType: 'json',
success: function (result) {
oTable.fnClearTable();
}});
};
url
在哪里http://PCNAME/
。
编辑
我什至在配置阶段设置了以下内容:
public override void Configure(Funq.Container container)
{
Plugins.Add(new CorsFeature());
RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
if (httpReq.HttpMethod == "OPTIONS")
httpRes.End();
});
base.SetConfig(new EndpointHostConfig
{
DefaultContentType = "application/json",
GlobalResponseHeaders = {
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
{ "Access-Control-Allow-Headers", "Content-Type, origin, accept" },
}
});
});
} // Configure