jQuery.get函数在Github Pages中不起作用 [英] jQuery.get function not working in Github Pages

查看:154
本文介绍了jQuery.get函数在Github Pages中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个项目页面设置,使用Github API自动填充Github中的项目详细信息。我已经把我的HTML和JavaScript文件放在gh-pages分支中。但是 jQuery.get()没有获取任何数据。我使用Firebug进行了检查,发现请求被发送并且响应代码为200 OK,但没有收到数据。 API没有问题,命令行 curl -i url 响应与预期一致。

我的HTML是:

 < h3> Project< / h3> 
< div id =projectname =sample-project>< / div>
< h3>撰稿人< / h3>
< p id =collab>< / p>

我的Javascript代码是:

<$ p ($ name);
var baseurl = $(document).ready(function(){
var project = $('#project')。attr https://api.github.com/;
var url = baseurl +repos / thamizha /;
url + = project;
url + =/ collaborators;
$ .get(url,function(data){
$('#collab')。text(JSON.stringify(data));
});
});

更新:我也试过。甚至没有提示。

  $。getJSON(url,function(data){
$('#collab') .html(JSON.stringify(data));
alert('Success');
});


解决方案

我没有试过这个,但你应该能够使用 $。getJSON()来完成跨域请求: http://api.jquery.com/jQuery.getJSON/ (签出 JSONP 部分)


如果URL包含字符串callback =? (或类似的,由
定义的服务器端API),请求将被视为JSONP。


- UPDATE -



我运行了测试,如果您尝试在不强制使用JSONP的情况下发出跨域请求,那么您会收到 200 OK 状态信息,但没有实际的回应。我还发现这个代码用于在jQuery中为JSONP设置AJAX请求:

  $。ajax({
dataType :'jsonp',
data:'id = 10',
jsonp:'jsonp_callback',
url:'http://myotherserver.com/getdata',
成功:function(){
// do stuff
}
});

这是: http://remysharp.com/2007/10/08/what-is-jsonp/


I am trying to build a project-page setup that auto populates the project details in Github using Github API. I have put up my HTML and JavaScript files in the gh-pages branch. But the jQuery.get() is not getting me any data. I checked with Firebug and found that the request gets sent and that response code is 200 OK, but no data is shown as received. There is no issue with the API, commandline curl -i url response is as expected.

My HTML is:

<h3>Project</h3>
<div id="project" name="sample-project"></div>
<h3>Contributors</h3>
<p id="collab"></p>

And my Javascript code is:

$(document).ready(function(){
    var project = $('#project').attr('name');
    var baseurl = "https://api.github.com/";
    var url = baseurl + "repos/thamizha/";
    url += project;
    url += "/collaborators";
    $.get(url, function(data){
            $('#collab').text(JSON.stringify(data));
        });
});

Update: I tried this too. No alert even.

$.getJSON(url, function(data){
$('#collab').html(JSON.stringify(data));
alert('Success');
});

解决方案

I have not tried this but you should be able to use $.getJSON() to do cross-domain requests: http://api.jquery.com/jQuery.getJSON/ (check-out the JSONP section)

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.

--UPDATE--

I ran a test and if you are trying to make a cross-domain request without forcing the use of JSONP then you will receive a 200 OK status message but no actual response. I also found this code for setting up an AJAX request in jQuery for JSONP:

$.ajax({
  dataType: 'jsonp',
  data: 'id=10',
  jsonp: 'jsonp_callback',
  url: 'http://myotherserver.com/getdata',
  success: function () {
    // do stuff
  }
});

This is from: http://remysharp.com/2007/10/08/what-is-jsonp/

这篇关于jQuery.get函数在Github Pages中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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