问题描述
我正在尝试使用 AJAX 调用 Yelp Fusion API,但出现以下错误.有人可以帮我弄清楚这里发生了什么吗?
api.yelp.com/v3/:1 加载资源失败:服务器响应状态为 403 ()index.html:1 在 'https://api.yelp.com/v3/' from origin 'null' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头.>
这是我正在使用的代码:
var queryURL = "https://api.yelp.com/v3/";var apiKey = "我的钥匙"$.ajax({网址:查询网址,方法:获取",标题:{接受":应用程序/json","Access-Control-Allow-Origin":"*",授权":`Bearer ${apiKey}`}}).then(函数(res){var 结果 = res.data控制台日志(结果);});
尝试使用 CORSAnywhere 代理,将您的密钥插入下面的片段并试一试:
//JavaScript 文档var queryURL = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/";var apiKey = "我的钥匙";$.ajax({网址:查询网址,方法:GET",标题:{接受":应用程序/json",x-requested-with":xmlhttprequest",访问控制允许来源":*",授权":`Bearer ${apiKey}`},成功:功能(结果){控制台日志(结果);}});
I'm trying to call the Yelp Fusion API using AJAX but I'm getting the following error below. Could someone help me figure out what's going on here?
api.yelp.com/v3/:1 Failed to load resource: the server responded with a status of 403 ()index.html:1 Access to XMLHttpRequest at 'https://api.yelp.com/v3/' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Here's the code I'm using:
var queryURL = "https://api.yelp.com/v3/";
var apiKey = "my key"
$.ajax({
url: queryURL,
method: "GET",
headers: {
"accept": "application/json",
"Access-Control-Allow-Origin":"*",
"Authorization": `Bearer ${apiKey}`
}
}).then(function(res) {
var results = res.data
console.log(results);
});
Try using the CORSAnywhere proxy, plug your key in the snip below and give it a shot:
// JavaScript Document
var queryURL = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/";
var apiKey = "my key";
$.ajax({
url: queryURL,
method: "GET",
headers: {
"accept": "application/json",
"x-requested-with": "xmlhttprequest",
"Access-Control-Allow-Origin":"*",
"Authorization": `Bearer ${apiKey}`
},
success: function(result){
console.log(result);
}
});
这篇关于如何解决 Yelp API 调用中的 CORS 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!