Python Eve:请求的资源上不存在“Access

编程入门 行业动态 更新时间:2024-10-22 11:02:19
本文介绍了Python Eve:请求的资源上不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我使用 Python EVE 框架编写了一个 API.在尝试从 AngularJS 应用程序访问 API 时,它显示如下所示的错误:

I have written an API using Python EVE framework. While trying to access the API from an AngularJS app it shows an error as shown below :

XMLHttpRequest cannot load http://127.0.0.1:5000/user/jay3dec. Request header field Authorization is not allowed by Access-Control-Allow-Headers. 

为了更正上述错误,我在 settings.py 中添加了以下内容

In order to correct the above error I added the following to the settings.py

X_DOMAINS = '*'
X_HEADERS = 'Authorization'

现在上面的错误消失了,控制台中显示了一个新的错误:

Now the above error disappears and a new error shows in the console :

XMLHttpRequest cannot load http://127.0.0.1:5000/user/jay3dec. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. 

但我已经在 settings.py 中添加了 X_DOMAINS = '*' .

But I have already added the X_DOMAINS = '*' in my settings.py .

这是angularjs代码:

Here is the angularjs code:

    $scope.validate = function() {

    var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.password);

    $http.defaults.headers.put = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': '*'
    };
    $http.defaults.headersmon['Authorization'] = 'Basic ' + encodedUserNameAndPassword;

    $http({
        method: 'GET',
        url: 'http://127.0.0.1:5000/user/jay3dec'
    }).
    success(function(data, status, headers, config) {
        console.log(data);
    }).
    error(function(data, status, headers, config) {
        alert(data);

    });
}

有没有发现可能是什么问题

Can any spot what may be the issue

推荐答案

FWIW,我遇到了类似的问题.

FWIW, I had similar problem.

然后我了解到只有模式定义的 API 才能获得 X_DOMAINS 标头的效果.如果您使用 app = Eve(settings=blah)@app.route() 之类的东西自己定义的 api,则不会受到 X_DOMAINS 标头的影响.因此,在这种情况下,您需要编写自己的装饰器.

Then I learned that only schema defined APIs will get the effect of X_DOMAINS header. The api if any you define on your own using something like app = Eve(settings=blah) and the @app.route() will not be affected by the X_DOMAINS header. So, in that case, you need to write your own decorator.

这篇关于Python Eve:请求的资源上不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 07:28:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/932718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上不   资源   Python   Eve   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!