Google Place API - 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问原点“null”[重复]

2024-03-19

我正在使用 Google Place API。

我想获得类型帮助的地方的建议。

所以,我所做的是——

var Google_Places_API_KEY = "AIzaSyAK08OEC-B2kSyWfSdeCzdIkVnT44bcBwM";      //Get it from - https://code.google.com/apis/console/?noredirect#project:647731786600:access
var language = "en";        //'en' for English, 'nl' for Nederland's Language


var Auto_Complete_Link = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+Google_Places_API_KEY+"&types=geocode&language="+language+"&input=Khu";
$.getJSON(Auto_Complete_Link , function(result)
{
    $.each(result, function(i, field)
    {
        //$("div").append(field + " ");
        //alert(i + "=="+ field);
        console.error(i + "=="+ field);
    });
});

所以我请求的链接是 -

如果我用浏览器访问此链接,我可以获得类似的输出(请尝试检查)-

但如果我尝试使用 jQuery.getJSON or .ajax,我的请求被此消息阻止-

222.

所以 XMLHTTPRequest 被阻止是因为 -

请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“null”。

我已经签到了堆栈溢出 https://stackoverflow.com/对于这个问题的解决方案并通过here https://stackoverflow.com/questions/20518524/no-access-control-allow-origin-header-is-present-on-the-requested-resource-or and here https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource,但无法完美地得到我的解决方案。

谁能启发我吗?


在找到 @sideshowbarker 的答案后,我得到了它的工作:

尝试从 REST API 获取数据时,请求的资源上不存在“Access-Control-Allow-Origin”标头 https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141

然后使用这种方法让它工作:

const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=500&key=[API KEY]"; // site that doesn’t send Access-Control-*
fetch(proxyurl + url) // https://cors-anywhere.herokuapp.com/https://example.com
.then(response => response.json())
.then(contents => console.log(contents))
.catch(() => console.log("Can’t access " + url + " response. Blocked by browser?"))

更多信息可以在上面链接的答案中找到。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Place API - 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问原点“null”[重复] 的相关文章

随机推荐