【发布时间】:2017-08-17 00:24:38
【问题描述】:
从我的其他项目调用 WebApi 时出现此错误。
XMLHttpRequest cannot load http://localhost:64678/api/Employees/RetriveAllEmployees. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49169' is therefore not allowed access. The response had HTTP status code 500.
我知道 CORS 需要在 WebApi 项目中启用。我已经在我的 WebApi 项目中添加了以下代码。
EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
但从我的其他项目调用 WebApi 时,我仍然收到此错误。
我像下面这样调用 webapi。
$("#getEmployees").on("click", function () {
$.ajax({
type: 'GET',
url: 'http://localhost:64678/api/Employees/RetriveAllEmployees',
success: function (data) {
debugger;
},
error: function (error) {
debugger;
}
});
});
无法理解问题所在。
【问题讨论】:
-
您使用的是 iisexpress 还是常规 iis?
标签: c# asp.net-web-api asp.net-mvc-5