2017-01-08 4 views
3

Ich habe diesen API-Aufruf in Postman Arbeit zu tun, aber wenn ich den Code in meinen JS-Code kopieren bekomme ich folgende Fehlermeldung:Ich versuche, einen Yelp Fusion (V3) API-Aufruf in JavaScript

XMLHttpRequest cannot load https://api.yelp.com/v3/businesses/search?term=restaurant&latitude=40.82783908257346&longitude=-74.10162448883057 .
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.
The response had HTTP status code 500.

Mein AJAX-Aufruf (mit Bearer für Sicherheit geändert):

var settings = { 
    "async": true, 
    "crossDomain": true, 
    "url": "https://api.yelp.com/v3/businesses/search?term=restaurant&latitude=40.82783908257346&longitude=-74.10162448883057", 
    "method": "GET", 
    "headers": { 
    "authorization": "Bearer xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    "cache-control": "no-cache", 
    // "postman-token": "1c66878e-c740-e10d-8d9a-71d731547d2e" 
    } 
} 


$.ajax(settings).done(function (response) { 
    console.log(response); 

Gemäß der Dokumentation Yelp - Yelp nicht CORS unterstützen - so CORS ist nicht das Problem.

+0

Mögliches Duplikat von [Wie CORS ermöglichen?] (Http://stackoverflow.com/questions/7067966/how-to-allow-cors) –

Antwort

0

Ich konnte es ohne Probleme auf der Client-Seite mit Fetch implementieren. Ich vermute, es könnte einer Ihrer Header sein.

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

die URL zu dieser Antwort falsch ist, wird mit der Frage neueste yelp-fusion-API (v3). – mting923