我正在尝试通过将数据拉入网页来利用 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>
如果我直接通过浏览器访问 URL (http://username.beanstalkapp.com/api/changesets.json)它工作得很好并返回 json。但是,我无法让 AJAX 返回它。任何帮助表示赞赏。谢谢!
您需要为跨域 ajax 请求创建代理。
通常的场景是这样的:
- 客户端向服务器发送ajax请求
- 您的服务器将请求转发到外部/远程服务器
- 等待远程服务器的响应
- 解析并处理来自远程服务器的响应
- 将响应发送回客户端
如果您使用 php,则可以使用curl 发送请求,并且实现起来非常容易。我最近写了关于这个主题的文章http://www.svlada.com/proxy-ajax-requests-curl-and-symfony-2/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)