【问题标题】:Issue "XMLHttpRequest cannot load" in Angularjs and WebApi [duplicate]在 Angularjs 和 WebApi 中问题“XMLHttpRequest 无法加载”[重复]
【发布时间】:2017-04-28 01:04:07
【问题描述】:

我创建了 asp.net webApi 并在 somee.com 上发布。我输入链接 xxxx.somee.com/api/xxxx 就可以了。但是我调用 Angularjs 不运行

$http.get('http://xxxxxx.somee.com/api')
.then(function(response) {
    console.log(response);
});

我收到错误:

XMLHttpRequest 无法加载 xxxx.somee.com/api/xxxx。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'null'。

请给出解决方案。非常感谢。

【问题讨论】:

  • 请在询问之前尝试搜索该特定错误。在这个网站和整个网络上有成千上万个关于它的问题

标签: javascript angularjs asp.net-web-api


【解决方案1】:

如果您的 Angular 网站和 API 网站在不同的域或端口中运行,则会发生此问题。 要解决此问题,请将以下代码添加到您的 webapiconfig.cs 文件中。

 var cors = new EnableCorsAttribute(http(s):// xxxx.somee.com/api/xxxx, "*", "*");
 config.EnableCors(cors);

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 2014-10-07
    • 2014-05-30
    • 2016-04-14
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多