jQuery的AJAX跨域使用基本身份验证 [英] jQuery AJAX Cross Domain with BASIC Authentication
问题描述
我试图利用魔豆(beanstalkapp.com)API通过提取数据到一个网页,使人们可以查看它没有访问我的SVN。
I'm attempting to make use of the Beanstalk (beanstalkapp.com) API by pulling data into a webpage so people can view it without accessing my SVN.
我在做什么,试图访问它是通过使用jQuery的AJAX请求。在code是低于,但每次我得到一个错误,而不能返回数据。
What I'm doing to try and access it is by using an AJAX request through jQuery. The code is below, but I get an error each time, and can't return the data.
<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 )它工作得很好,并返回JSON。但是,我不能让AJAX退货。任何帮助是AP preciated。谢谢!
If I access the URL straight through my browser (http://username.beanstalkapp.com/api/changesets.json) it works just fine and returns the json. However, I cannot get the AJAX to return it. Any help is appreciated. Thanks!
推荐答案
您将需要为跨域Ajax请求代理。
You will need to make proxy for cross-domain ajax requests.
通常的情形是这样的:
- 在客户端发送Ajax请求到服务器
- 您的服务器将请求转发给外部/远程服务器
- 在等待来自远程服务器的响应
- 解析和远程服务器的过程响应
- 发送响应返回给客户端
如果你正在使用PHP,您可以发送请求的卷曲,它是pretty的容易实现。 // WWW:我对这个话题最近 HTTP写的文章。 svlada.com/proxy-ajax-requests-curl-and-symfony-2/ 。
If you are using php you can send requests with curl, and it is pretty easy to implement. I have wrote article on this topic recently http://www.svlada.com/proxy-ajax-requests-curl-and-symfony-2/.
这篇关于jQuery的AJAX跨域使用基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!