【问题标题】:jQuery JSON API call not working?jQuery JSON API 调用不起作用?
【发布时间】: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 响应。
  • 添加第二个?&amp;callback=? 有帮助吗? &amp;callback=??这是什么版本的jquery
  • @KevinB 直接这样吧? api.syfaro.net/minecraft/1.2/server/…

标签: javascript jquery ajax json api


【解决方案1】:

响应看起来不像一个数组。它只是一个对象。尝试只使用 json.status,而不是 json[0].status。

【讨论】:

    【解决方案2】:

    试试这个网址:

    http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=jsonp
    

    您的原始网址只是 ?返回开头没有函数名。我不确定没有函数名的jsonp是否可行,但你可以检查...

    在这里工作:

    http://jsfiddle.net/yj467/1/

    $.getJSON( "http://api.syfaro.net/minecraft/1.2/server/status?ip=mc.hypixel.net&callback=?", function( json ) {
      console.log( json );
      $(".status").html(json.status);
    });
    

    【讨论】:

    • jQuery 应该为你填写那部分
    • 不起作用。我敢肯定,如果没有回调,它甚至都无法工作。
    • 你想要callback=?。 jQuery 会将? 替换为生成的函数名。
    • @Alyn 检查我的 jsfiddle,简化所有工作正常!
    • @salivan 确实有效!我一定是做错了什么
    【解决方案3】:

    编辑: 我阅读了 getJSON 的文档,似乎它确实会自动识别 JSONP 请求。把帖子留在这里,以防它对某人有帮助

    我怀疑你被Same-origin policy 搞砸了。

    如果您(我怀疑)实际上不是 syfaro.net 的程序员,并且您没有在该服务器上运行此脚本,您将无法访问该页面。

    帮助解决这个问题,人们创建了 JSONP,这是一种相当讨厌的破解方式,但可以让你做你想做的事。

    你可以这样使用它:

    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
    
        var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= 'http://api.syfaro.net/minecraft/1.2/server/status?ip="+ ip + "&callback=jsonCallback';
        head.appendChild(script);
      }
    }
    
    function jsonCallback(json){
      $("#addmes").html("<h1>" + json[0].status + "</h1>"); //printing out the API status message
    }
    

    http://jsfiddle.net/3VZuP/

    【讨论】:

    • 这就是 jquery 在幕后所做的。
    • 使用 JSONP。 callback=? 告诉 jQuery 为你做这件事。
    • 此解决方案不起作用,因为响应未设置变量。您将使用未分配给变量的对象导入脚本,因此无法引用它。
    • @bic 不正确,它将以所述对象作为参数执行函数,jquery 定义所述函数并将其名称传递给回调参数。此答案中的代码将起作用,但我怀疑它与问题有关。
    猜你喜欢
    • 2014-07-27
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多