【发布时间】:2016-05-21 15:30:59
【问题描述】:
好吧,当它在控制台中出现此错误时,我真的很讨厌它。而且我知道 stackoverflow 充斥着这些类型的问题。但是,我已经完成了研究,并且在我的 Web API 2 Web 服务中启用了 CORS,但我仍然收到此错误。
这是我的 Web API 2 代码:
namespace WebApi.App.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ServiceController : ApiController
{
[HttpGet]
[Route("GetData")]
public IHttpActionResult GetEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA);
}
[HttpPost]
[Route("PostData")]
public IHttpActionResult PostEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA.theID);
}
}
public class DATAvars
{
public string theID { get; set; }
public string empImg { get; set; }
}
}
与
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
与
namespace WebApi.App
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.MapHttpAttributeRoutes();
config.EnableCors();
}
}
}
与
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods" , "GET, POST" );
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers" , "Content-Type, Accept" );
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
}
然后是我的 AJAX 调用代码(托管在另一个域上):
$.ajax({
type: "POST",
crossDomain: true,
url: "http://dev-blahblah/newWS/PostData",
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
},
data: {
theID: "2135648792",
empImg: "false"
},
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
}
});
加载资源失败:服务器响应状态为 404(未找到) index.html:1 XMLHttpRequest 无法加载http://dev-blahblah/newWS/PostData。预检响应包含无效的 HTTP 状态代码 404
我已经花了 DAYS 天试图弄清楚这一点,并没完没了地在谷歌上搜索示例,但似乎所有示例都不起作用。
我会非常感谢有人让我知道我需要做什么才能使用 JQUERY AJAX。
-在 CHROME = WORKS 中的同一域上运行它
-在 CHROME 中的不同域上运行它 = 不起作用
-在 IE = WORKS 的同一个域上运行它
-在 IE = WORKS 的不同域上运行它
【问题讨论】:
-
_begirRequest 中的那一行应该是 response.flush() 而不是 .end()?这就是我使用的,它发回 200。不确定 .end() 的作用。
-
还有。 Allow-meathods 不包括 OPTIONS,我认为它需要它。
-
@bri 感谢您的建议,但它们导致了同样的错误。
-
你看到this post了吗?似乎是说您应该选择一个位置来设置这些标头(web.config 或 beginRequest 但不能同时设置)...仍在试图弄清楚您如何在该响应中以状态 0 而不是 200 结束...
-
@bri 很遗憾这也没有帮助
标签: javascript c# ajax asp.net-web-api cors