我正在Udacity的Web开发Nanodegree课程中开展一个项目 . 我决定使用餐馆信息并生成我的API密钥 . 但是,当我在AJAX对象中使用它来获取数据时,开发工具会向我显示“无效API”错误 . 但是,API是正确的 . 请帮忙 .
这是我的代码:
$(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"
网址:
https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID
请注意,我使用的餐厅ID和API是正确的 . 我对PHP知之甚少,所以我不知道curl的意思 .
1 回答
请尝试以下代码:
但是,由于安全问题,不鼓励使用上述方法 . 您可以通过在服务器端编写处理程序来使用PHP中的服务器到服务器调用来访问Zomato API,该处理程序可以作为ajax调用从按钮单击事件中调用 . 这是示例PHP处理程序: