在所请求的资源上没有“Access-Control-Allow-Origin”头。因此,不允许原始'http:// localhost:22002'访问 [英] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:22002' is therefore not allowed access

查看:416
本文介绍了在所请求的资源上没有“Access-Control-Allow-Origin”头。因此,不允许原始'http:// localhost:22002'访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对这个错误NoAccess-Control-Allow-Origin'头在请求的资源上存在,因此'localhost URL'是不允许访问的。我已启用cors,但它仍然给我错误。

I am facing this error "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost URL' is therefore not allowed access." i have enabled cors but it is still giving me error.

function makeApiCall() {
            $.ajax({
                url: "https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&redirect_uri="+URL+"&response_type=code&client_id=" + clientId + "&access_type=offline",
                type: "POST",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                    xhr.setRequestHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
                    xhr.setRequestHeader("Content-Type", "text/plain");
                    xhr.setRequestHeader("Access-Control-Allow-Credentials", "true");
                    }

            });
            }


推荐答案

同一问题(当尝试交换令牌的访问代码时),问题是您以客户端方式模仿服务器端验证方法。

I have just overcome the exact same issue (when trying to exchange the access code for a token), the problem is that you are imitating a server-side auth approach in a client-side manner.

最简单的方法是使用PHP处理这个。或者更容易,如果你不需要自己编写这些HTTP请求,请使用JS客户端库。

The easiest way to over come this would be to use PHP to handle this for you. Or easier still, if you do not need to be writing these HTTP Requests yourself, use the JS client library.

这篇关于在所请求的资源上没有“Access-Control-Allow-Origin”头。因此,不允许原始'http:// localhost:22002'访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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