2016-11-16 8 views
1

私はUdacityのWeb開発Nanodegreeコースでプロジェクトを進めています。私はレストラン情報を使用し、APIキーを生成することに決めました。しかし、データを取得するためにAJAXオブジェクトで使用すると、devツールで「無効なAPI」エラーが表示されます。 APIは、しかし、正しいです。助けてください。ここでZomato APIにアクセスできないJSONオブジェクト

は私のコードです:

$(document).ready(function(){ 
$("button").click(function(){ 
    $.get("https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID", { "Accept": "application/json", "user-key": 'MY_API_KEY' }, function(data, status){ 
     alert("Data: " + data + "\nStatus: " + status); 
    }); 
}); 

});ここで

がZomatoのドキュメントです:

カール:

curl -X GET --header "Accept: application/json" --header "user-key: MY_API" "https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID" 

URL:

https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID 

私が使用したレストランのIDとAPIが正しかったことに注意してください。私は多くのPHPを知らないので、カールが何を意味するのか分かりません。

答えて

3

コードの下の部分を試してください:

$(document).ready(function(){ 
    $("button").click(function(){ 
    $.get("https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID&apikey=MY_API_KEY", function(data, status){ 
     alert("Data: " + data + "\nStatus: " + status); 
    }); 
    }); 
}); 

しかし、上記の方法は、セキュリティ上の問題のために推奨されていません。 aomaxコールとしてボタンクリックイベントから呼び出すことができるサーバー側にハンドラを記述することで、PHPでサーバー間呼び出しを使用してZomato APIにアクセスできます。サンプルのPHPハンドラは次のとおりです:

$curl_channel = curl_init('https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID&apikey=MY_API_KEY'); 
curl_setopt($curl_channel, CURLOPT_RETURNTRANSFER, true); 
$curl_response = curl_exec($curl_channel); 
curl_close($curl_channel); 
//Convert output to object 
$curl_output = json_decode($curl_response); 
関連する問題