我正在尝试使用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错误?
发布评论