无法访问Zomato api JSON对象
Cannot access Zomato APIs JSON object
我正在Udacity的web开发纳米学位课程中的一个项目上工作。我决定使用餐厅信息并生成我的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"
URL: https://developers.zomato.com/api/v2.1/restaurant?res_id=MY_RES_ID
请注意,我使用的餐厅ID和API是正确的。我不太懂PHP,所以不知道curl是什么意思
请尝试下面的代码:
$(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);
});
});
});
但是,由于安全问题,不鼓励使用上述方法。通过在服务器端编写处理程序,您可以在PHP中使用服务器对服务器调用访问Zomato API,该处理程序可以作为ajax调用从按钮单击事件调用。下面是示例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);
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象