如何解决Yelp API调用中的CORS错误? [英] How do I resolve the CORS error in Yelp API call?

查看:96
本文介绍了如何解决Yelp API调用中的CORS错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

api.yelp.com/v3/:1无法加载资源:服务器响应状态为403()index.html:1在' https://api.yelp.com/v3/"已被CORS政策阻止:对预检请求的响应未通过访问控制检查:所请求的资源上没有"Access-Control-Allow-Origin"标头.

这是我正在使用的代码:

  var queryURL ="https://api.yelp.com/v3/";var apiKey =我的钥匙"$ .ajax({网址:queryURL,方法:"GET",标头:{"accept":"application/json",访问控制允许来源":"*",授权":`Bearer $ {apiKey}`}}).then(function(res){var结果= res.dataconsole.log(结果);}); 

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

 //JavaScript文档var queryURL ="https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/";var apiKey =我的钥匙"$ .ajax({网址:queryURL,方法:"GET",标头:{"accept":"application/json","x-requested-with":"xmlhttprequest",访问控制允许来源":"*",授权":`Bearer $ {apiKey}`}}).then(function(res){var结果= res.dataconsole.log(结果);}); 

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

这篇关于如何解决Yelp API调用中的CORS错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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