JSON 跨域错误
JSON Cross-Domain Error
伙计们...我已经尝试了很多来解决这个问题,在这里和其他网站上找到了一些解决方案...但我没有找到解决这个问题的方法!
那么真正的问题是什么..很简单我有这个代码:
function refreshRadioStats()
{
jQuery.support.cors = true;
$.get('http://raphaleao.com/json', function(data){
var templateStats = '<span class="glyphicon glyphicon-music"></span> '+ j_lang[0] +
'<marquee><h4><i>'+data.musica_atual+'</i></h4></marquee><br>'+
'<span class="glyphicon glyphicon-headphones"></span> '+j_lang[1]+': <b>'+data.ouvintes_conectados+
'</b><br>'+
'<span class="glyphicon glyphicon-user"></span> '+j_lang[3]+': <b>'+locutor+'</b><br><br>'+
'<button type="button" class="btn btn-info" id="refreshstadistics" style="width:100%"><span class="glyphicon glyphicon-refresh"></span> '+ j_lang[2] +'</button>';
$('#resultse').append(templateStats);
},'json');
}
好的,有什么问题吗?
问题是:该脚本显然无法访问网站 http://raphaleao.com/json 并且为此不起作用。
问题:
XMLHttpRequest cannot load http://raphaleao.com/json. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'Stack don't allow me to post more than 2 links' is therefore not allowed access.
该脚本有效,因为我已经在 JSON 中的其他站点进行了测试并且效果很好。我已经把这个放在 web.config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
同样的问题,大家能帮我解决一下吗?
我已修复
好吧,在尝试了一些东西之后,@mattferderer 打开了我的思路,我想通了这一切是怎么回事...
首先,我删除了 web.config 一侧,只留下 header() 函数。好的,第一个问题已解决(笑);
然后,我想通了为什么 $.getJSON() 不加载其他网站的信息...很简单,只是添加(至少我在 PHP 中做到了)这个:
header('Content-Type: text/javascript');
在我的例子中,页面的格式是 text/html,所以 $.getJSON() 将无法加载该页面中的所有信息!
谢谢你们,抱歉英语不好!
尝试从 web.config 中删除 access-control-allow-origin,因为问题是 access-control-allow-origin 被使用了两次。如果您查看您的回复 Headers,它会显示:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.5.11
Access-Control-Allow-Origin: *
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Wed, 06 Apr 2016 14:43:54 GMT
Content-Length: 387
伙计们...我已经尝试了很多来解决这个问题,在这里和其他网站上找到了一些解决方案...但我没有找到解决这个问题的方法!
那么真正的问题是什么..很简单我有这个代码:
function refreshRadioStats()
{
jQuery.support.cors = true;
$.get('http://raphaleao.com/json', function(data){
var templateStats = '<span class="glyphicon glyphicon-music"></span> '+ j_lang[0] +
'<marquee><h4><i>'+data.musica_atual+'</i></h4></marquee><br>'+
'<span class="glyphicon glyphicon-headphones"></span> '+j_lang[1]+': <b>'+data.ouvintes_conectados+
'</b><br>'+
'<span class="glyphicon glyphicon-user"></span> '+j_lang[3]+': <b>'+locutor+'</b><br><br>'+
'<button type="button" class="btn btn-info" id="refreshstadistics" style="width:100%"><span class="glyphicon glyphicon-refresh"></span> '+ j_lang[2] +'</button>';
$('#resultse').append(templateStats);
},'json');
}
好的,有什么问题吗? 问题是:该脚本显然无法访问网站 http://raphaleao.com/json 并且为此不起作用。 问题:
XMLHttpRequest cannot load http://raphaleao.com/json. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'Stack don't allow me to post more than 2 links' is therefore not allowed access.
该脚本有效,因为我已经在 JSON 中的其他站点进行了测试并且效果很好。我已经把这个放在 web.config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
同样的问题,大家能帮我解决一下吗?
我已修复 好吧,在尝试了一些东西之后,@mattferderer 打开了我的思路,我想通了这一切是怎么回事...
首先,我删除了 web.config 一侧,只留下 header() 函数。好的,第一个问题已解决(笑);
然后,我想通了为什么 $.getJSON() 不加载其他网站的信息...很简单,只是添加(至少我在 PHP 中做到了)这个:
header('Content-Type: text/javascript');
在我的例子中,页面的格式是 text/html,所以 $.getJSON() 将无法加载该页面中的所有信息!
谢谢你们,抱歉英语不好!
尝试从 web.config 中删除 access-control-allow-origin,因为问题是 access-control-allow-origin 被使用了两次。如果您查看您的回复 Headers,它会显示:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.5.11
Access-Control-Allow-Origin: *
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Wed, 06 Apr 2016 14:43:54 GMT
Content-Length: 387