gpt4 book ai didi

node.js - CORS 错误 Angular 2 + Express Node

转载 作者:搜寻专家 更新时间:2023-11-01 00:28:46 24 4
gpt4 key购买 nike

正面( Angular 2)

public googlesearch(ev:Event){
this.http.get("https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ev.target.value+"&types=establishment&key=AIzaSyAKdEt_dYbdPY-CFo0zie23E44XxTQc1n7").map(res => $.parseJSON(res)).subscribe( data => {
this.predictions = data.predictions;
console.log(this.predictions);
},(error) =>{
alert('Not able to get')
} )


}

Node (Express 4.14)

var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

但是我得到了 CORS 错误

Failed to load https://maps.googleapis.com/maps/api/place/autocomplete/json?input=loc&types=establishment&key=AIzaSyAKdEt_dYbdPY-CFo0zie23E44XxTQc1n8: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

并且在 npm 控制台也没有收到任何请求。

最佳答案

该 header 必须由目标 url 服务器设置,在您的情况下为 https://maps.googleapis.com/。在您的服务器上设置 header 将无济于事。

你可以,但是-

  1. 找到允许从浏览器调用的适当 API。
  2. 在您的服务器上实现该 url 的代理并从浏览器调用它。

关于node.js - CORS 错误 Angular 2 + Express Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46758154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com