【发布时间】:2013-12-19 15:07:18
【问题描述】:
我正在尝试对 Minecraft API 进行 API 调用,该 API 会与其他 API 一起返回状态消息,然后将其打印在网站上。为此,我使用 AJAX JSON 调用,获取输入的字符串以连接到 API 并在函数中获取有关服务器的信息(我很确定这是最常用的方法)。
但是,当我尝试输入(工作)IP 时,它会非常简短地显示 researching data... 消息,然后再次清除该字段。什么都没有出现,即使应该有来自 API 的状态消息。它甚至不登录控制台。
有什么帮助吗?
JS代码:
function getStatus(){
var ip = $("#ip").val(); //getting the value from the textbox
if(ip == ""){
$("#addmes").html("<h1>you didn't enter anything -.-</h1>"); //checking if anything is entered
} else {
$("#addmes").html("<h1>researching data...</h1>"); //displaying a temporary status message
$.getJSON("http://api.syfaro.net/minecraft/1.2/server/status?ip="+ ip + "&callback=?", function(json){ //connecting to the API, using the IP variable in the process
console.log(json); //console log
$("#addmes").html("<h1>" + json[0].status + "</h1>"); //printing out the API status message
})
}
}
API 响应(使用 http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=? 测试):
?({"status":"success","ip":"mc.hypixel.net","port":25565,"last_update":"2013-12-19 09:09:58","online":true,"motd":"\u00a7aHypixel Lobby \u00a76| \u00a7cPlay Now! \u00a7eMega Walls \u00a7aPublic BETA!","players":{"max":16001,"online":6989,"list":false},"version":"1.7.2","favicon":false});
【问题讨论】:
-
直接导航到 url+ip(使用您的网络浏览器)并在此处发布返回的内容。
-
url直接在浏览器中有效吗?它返回什么?
-
好的,我会发布 API 响应。
-
添加第二个
?到&callback=?有帮助吗?&callback=??这是什么版本的jquery -
@KevinB 直接这样吧? api.syfaro.net/minecraft/1.2/server/…
标签: javascript jquery ajax json api