我正在尝试用JavaScript进行YelpFusion(V3)API调用

我正在尝试用JavaScript进行YelpFusion(V3)API调用,javascript,ajax,Javascript,Ajax,我在Postman中使用了此API调用,但当我将代码复制到JS代码中时,出现以下错误: 无法加载XMLHttpRequest 对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头 因此,不允许访问源“null” 响应的HTTP状态代码为500 我的AJAX呼叫(为安全起见更改了承载): 根据Yelp文档,Yelp不支持CORS,因此CORS不是问题所在。我能够使用Fetch在客户端实现它,没有任何问题。我怀疑这可能是你的头球之一 fetch('https://api.

我在Postman中使用了此API调用,但当我将代码复制到JS代码中时,出现以下错误:

无法加载XMLHttpRequest
对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头
因此,不允许访问源“null”
响应的HTTP状态代码为500

我的AJAX呼叫(为安全起见更改了承载):


根据Yelp文档,Yelp不支持CORS,因此CORS不是问题所在。

我能够使用Fetch在客户端实现它,没有任何问题。我怀疑这可能是你的头球之一

fetch('https://api.yelp.com/oauth2/token?client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: JSON.stringify({
    grant_type: 'client_credentials',
  })
})
  .then((res) => res.json())
  .then((resJSON) => {
    this.setState({ access_token: resJSON.access_token });
    console.log(resJSON)
  })
  .then(() => {
    fetch('https://api.yelp.com/v3/businesses/search?location=12345', {
      method: 'GET',
      headers: {
        'authorization': 'Bearer ' + this.state.access_token
      }
    })
    .then((res) => res.json())
    .then((resJSON) => {
      console.log('resJSON', resJSON)
    })
  })
  .catch((err) => {
    console.log('err', err);
  })
fetch('https://api.yelp.com/oauth2/token?client_id=&client_secret=', {
方法:“POST”,
标题:{
“内容类型”:“应用程序/x-www-form-urlencoded”,
},
正文:JSON.stringify({
授予类型:“客户端凭据”,
})
})
.然后((res)=>res.json())
.然后((resJSON)=>{
this.setState({access_token:resJSON.access_token});
log(resJSON)
})
.然后(()=>{
取('https://api.yelp.com/v3/businesses/search?location=12345', {
方法:“GET”,
标题:{
“授权”:“承载人”+此.state.access\u令牌
}
})
.然后((res)=>res.json())
.然后((resJSON)=>{
log('resJSON',resJSON)
})
})
.catch((错误)=>{
console.log('err',err);
})

此答案中可能重复的url不正确,问题是使用最新的yelp fusion API(v3)。
fetch('https://api.yelp.com/oauth2/token?client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: JSON.stringify({
    grant_type: 'client_credentials',
  })
})
  .then((res) => res.json())
  .then((resJSON) => {
    this.setState({ access_token: resJSON.access_token });
    console.log(resJSON)
  })
  .then(() => {
    fetch('https://api.yelp.com/v3/businesses/search?location=12345', {
      method: 'GET',
      headers: {
        'authorization': 'Bearer ' + this.state.access_token
      }
    })
    .then((res) => res.json())
    .then((resJSON) => {
      console.log('resJSON', resJSON)
    })
  })
  .catch((err) => {
    console.log('err', err);
  })