[英]jQuery AJAX Cross Domain with BASIC Authentication
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. 我试图通过将数据拉入网页来使用Beanstalk(beanstalkapp.com)API,以便人们可以在不访问我的SVN的情况下查看它。
What I'm doing to try and access it is by using an AJAX request through jQuery. 我正在尝试访问它是通过jQuery使用AJAX请求。 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>
If I access the URL straight through my browser ( http://username.beanstalkapp.com/api/changesets.json ) it works just fine and returns the json. 如果我直接通过浏览器访问URL( http://username.beanstalkapp.com/api/changesets.json ),它可以正常工作并返回json。 However, I cannot get the AJAX to return it.
但是,我无法让AJAX返回它。 Any help is appreciated.
任何帮助表示赞赏。 Thanks!
谢谢!
You will need to make proxy for cross-domain ajax requests. 您需要为跨域ajax请求创建代理。
Usual scenario looks like this: 通常情况如下:
If you are using php you can send requests with curl, and it is pretty easy to implement. 如果你使用的是php,你可以用curl发送请求,而且很容易实现。 I have wrote article on this topic recently http://www.svlada.com/proxy-ajax-requests-curl-and-symfony-2/ .
我最近写了一篇关于这个主题的文章http://www.svlada.com/proxy-ajax-requests-curl-and-symfony-2/ 。
you cant get a json from other domain than yours. 你不能从其他领域得到一个json而不是你的。 this is a security issue called same origin policy to get over it use JSONP not JSON.
这是一个称为同源策略的安全问题,使用JSONP而不是JSON来克服它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.