【发布时间】:2016-10-31 12:40:40
【问题描述】:
我正在使用 AngularJS 将应用程序从 Django 移植到纯 HTML5/CSS,并且在向 TheTVDB REST API 服务器 (https://api.thetvdb.com/) 发出 JSON POST 请求时遇到问题。
API 调用正在 AngularJS 上的服务中完成:
return {
login: function(){
return $http({
method: 'POST',
dataType: 'json',
headers: {'Content-Type': 'application/json'},
data: {'apikey':apiKey, 'username': username, 'userkey': identifier},
url: tvdbAuthURL
})
}
}
然后我得到这个错误:
XMLHttpRequest cannot load https://api.thetvdb.com/login/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 401.
我已经尝试避免预先发送请求,但没有运气,因为我没有使用 python 请求库遇到这个问题,例如(我使用这个 python 库从运行 angular JS 的同一台机器触发对 tvDB 的请求应用程序完全没有问题)我想知道 AngularJS 中是否没有方法或不同的指令来阻止它设置 CORS 标头。
TheTVDB 不会将 CORS 添加到他们的服务器,因为他们的大多数用户都在运行 Laravel (PHP)、Java 和 Django(Python) 应用程序并且正在使用他们的服务而没有此类 CORS 问题。
非常感谢您的帮助。
谢谢你, 最好的祝福 TS
【问题讨论】:
-
在python中创建代理
-
出于架构原因,我想避免这种情况。但这是否意味着当服务器不提供 CORS 支持时我会受到 AngularJS 的限制?
-
不,这意味着您受到浏览器安全性的限制。除了使用第三方代理,您别无选择
-
你的api从body接收数据?还是通过参数?,也不是在标题中重复使用 dataType 吗?
-
@PauloGaldoSandoval 我会从服务器收到一个带有令牌信息的 json,以便我继续处理其他请求。 dataType 可能是多余的,但我想它在这里没有影响。
标签: javascript angularjs json rest cors