尝试通过https://api.thetvdb.com/向TVDB REST API发出JSON POST请求,但收到CORS错误 [英] Trying to make a JSON POST request to the TVDB REST API at https://api.thetvdb.com/, but getting CORS error

查看:1818
本文介绍了尝试通过https://api.thetvdb.com/向TVDB REST API发出JSON POST请求,但收到CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AngularJS将应用程序从Django移植到纯HTML5/CSS,并且遇到向TheTVDB REST API服务器发出JSON POST请求的问题(

I am porting an application from Django to purely HTML5/CSS with AngularJS and am facing issues making JSON POST requests to TheTVDB REST API server (https://api.thetvdb.com/).

API调用在AngularJS上的服务中完成:

The API call is being done in a service on 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 request lib遇到此问题,(我从运行有角JS的同一台机器上使用此python lib触发对tvDB的请求应用程序,完全没有问题)我想知道AngularJS中是否没有方法或其他指令来阻止它设置CORS标头.

I have tried avoiding pre-flighting the request but no luck and since I did not have this issue using python requests lib, for example, (I trigger request to the tvDB using this python lib from the same machine running the angular JS app with no problems at all) I wonder if there isn't a way or a different directive in AngularJS to keep it from setting CORS headers.

TheTVDB不会将CORS添加到其服务器中,因为大多数用户正在运行Laravel(PHP),Java和Django(Python)应用程序,并且使用其服务时不会出现此类CORS问题.

TheTVDB will not add CORS to their server as most of their users are running Laravel (PHP), Java and Django(Python) applications and are using their services with no such CORS problems.

非常感谢您的帮助.

谢谢你, 最好的祝福 TS

Thank you, Best Regards TS

推荐答案

您的Python请求有效,因为它来自您的服务器或本地计算机,并且

Your Python request works because it is coming from your server or local machine, and the same origin policy only applies inside a browser.

您最好的答案是使用服务器端脚本.让您的客户端POST请求转到服务器上的控制器或端点,然后触发服务器脚本.假设API有一些返回数据,您可以将其添加到服务器的响应中,然后将其发送给客户端.

Your best answer is to use a server-side script. Have your client side POST request go to a controller or endpoint on your server, which then triggers the server script. Assuming there is some return data from the API, you'd add that to your server's response and then send it to the client.

这篇关于尝试通过https://api.thetvdb.com/向TVDB REST API发出JSON POST请求,但收到CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆