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

编程入门 行业动态 更新时间:2024-10-26 16:28:55
本文介绍了如何解决Yelp API调用中的CORS错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

这是我正在使用的代码:

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

解决方案

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

//JavaScript文档var queryURL ="cors-anywhere.herokuapp/api.yelp/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/v3/:1 Failed to load resource: the server responded with a status of 403 () index.html:1 Access to XMLHttpRequest at 'api.yelp/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 = "api.yelp/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 = "cors-anywhere.herokuapp/api.yelp/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错误?

本文发布于:2023-10-12 02:47:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   错误   API   Yelp   CORS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!