【发布时间】:2014-11-16 00:03:49
【问题描述】:
我正在尝试通过将数据拉入网页来使用 Beanstalk (beanstalkapp.com) API,以便人们无需访问我的 SVN 即可查看它。
我正在尝试通过 jQuery 使用 AJAX 请求来访问它。代码如下,但每次都报错,无法返回数据。
<script type="text/javascript">
$(document).ready(function() {
var tok = 'username' + ':' + 'password123';
hash = btoa(tok);
authInfo = "Basic " + hash;
$.ajax({
url: "http://username.beanstalkapp.com/api/changesets.json",
beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", authInfo); },
type: "GET",
async: false,
crossDomain: true,
dataType: "json",
success: function(html){
console.log(html);
},
error: function(html){
console.log('error');
}
});
});
</script>
如果我直接通过浏览器 (http://username.beanstalkapp.com/api/changesets.json) 访问 URL,它就可以正常工作并返回 json。但是,我无法让 AJAX 返回它。任何帮助表示赞赏。谢谢!
【问题讨论】:
-
确保服务器/页面(或其他)允许通过 Ajax 进行请求。
-
当你通过 ajax 调用时你是如何提供用户名和密码的
-
Bishnu- 还有其他方法可以远程获取数据吗?
-
muthu- 在上面的脚本中。这是 var 'tok'。
-
我最终只是编写了一个 cURL 脚本来执行此操作。服务器必须不喜欢被 AJAX ping。如果有人想看,请告诉我,我会编辑问题。
标签: javascript jquery ajax cross-domain