Ajax调用Github的API失败 [英] Ajax call to Github API fails

查看:238
本文介绍了Ajax调用Github的API失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有点在这个新的,一个在code我到目前为止还没有成型,但如果有人能告诉我什么,我缺我将不胜感激。

So I'm a little new at this, a the code I have so far is not working yet, but if anyone can tell me what i missing I would be grateful.

基本上,我试图打电话到GitHub的API,它返回JSON数据。我想最终还是要分析它,并只显示具体的信息,但在present我只是试图让数据在浏览器中显示。这是我到目前为止有:

Basically, I'm trying to make a call to github's api, which returns json data. I'd eventually like to parse it and display only specific information, but at the present I'm just trying to get the data to show in my browser. Here is what I have so far:

<script type="text/javascript">
$(document).ready(function() {

$.ajax({
    url: "https://api.github.com/repos/VonC/gitolite/git/refs/tags",
    dataType: "jsonp", // I'm under the impression i should use jsonp, since this is a cross domain call
    success: function (returndata)
    {
        $('.result').html(returndata);
        alert('Load was performed.');
      }  
    });
 });
</script>

       

网址明确工​​作原理:当您使用curl调用它,将返回以下JSON数据:

The url definitely works: when you call it using CURL, the following json data is returned:

[
{
"object": {
  "type": "commit",
  "sha": "9accde83842523e18de320fc2f0a8efeaebef27b",
  "url": "https://api.github.com/repos/jeffreycwitt/jeffswebpage/git/commits/9accde83842523e18de320fc2f0a8efeaebef27b"
 },
"url": "https://api.github.com/repos/jeffreycwitt/jeffswebpage/git/refs/heads/master",
"ref": "refs/heads/master"
 } 
]

感谢您的建议,您可以给我。

Thanks for any advice you can give me.

推荐答案

的dataType 也许应该是 JSONP 而不是 jasonp 。更妙的是,它应该仅仅是 JSON 你不是做一个JSONP调用。

dataType should probably be jsonp and not jasonp. Even better, it should simply be json as you are not making a JSONP call.

您应该注意的另一件事是, returndata 将是实际的,解析散发出来的JSON再presentation,而不是JavaScript对象JSON对象的字符串。这意味着,你不能把它直入。结果 DIV。

Another thing you should watch out for is that returndata is going to be the actual, parsed JavaScript object that comes out of the JSON representation, not the JSON object as a string. This means that you cannot put it straight into the .result div.

下面,似乎为我工作:

$.ajax({
    url: "https://api.github.com/repos/VonC/gitolite/git/refs/tags",
    dataType: "json",
    success: function (returndata)
    {
        $("#result").html(returndata[0]["object"]["sha"]);
        alert('Load was performed.');
    }  
});

这篇关于Ajax调用Github的API失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆