如何解决Yelp API调用中的CORS错误?

时间:2018-11-18 04:27:30

标签: api yelp

我正在尝试使用AJAX调用Yelp Fusion API,但下面出现以下错误。有人可以帮我弄清楚这里发生了什么吗?

api.yelp.com/v3/:1无法加载资源:服务器响应状态为403() index.html:1已从CORS策略阻止从原点“空”访问“ https://api.yelp.com/v3/”处的XMLHttpRequest:对预检请求的响应未通过访问控制检查:否“ Access-Control-Allow-Origin”标头出现在请求的资源上。

这是我正在使用的代码:

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);
 });

1 个答案:

答案 0 :(得分:0)

尝试使用CORSAnywhere代理,将您的密钥插入下面的代码片段中并进行尝试:

// 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}`
     }
 }).then(function(res) {
     var results = res.data
     console.log(results);
 });