简体   繁体   English

JSON跨域错误

[英]JSON Cross-Domain Error

Well guys... I've tried lots to fix this problem, catching some solutions here and in others Sites... But i don't get the solution to this problem! 大家好...我已经尝试了很多解决此问题的方法,在这里和其他站点中都找到了一些解决方案...但是我没有解决此问题的方法!

Well what's the real problem.. It's simple i have this code: 那么真正的问题是什么..很简单,我有以下代码:

    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');
}

Ok, what's the problem? 好的,怎么了? The problem is: The script apparently can't acess the website http://raphaleao.com/json and for this, doesn't work. 问题是:该脚本显然无法访问网站http://raphaleao.com/json ,因此无法正常工作。 The problem: 问题:

XMLHttpRequest cannot load http://raphaleao.com/json . XMLHttpRequest无法加载http://raphaleao.com/json The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. “ Access-Control-Allow-Origin”标头包含多个值“ *,*”,但只允许一个。 Origin 'Stack don't allow me to post more than 2 links' is therefore not allowed access. 因此,不允许访问起源“堆栈不允许我发布两个以上的链接”。

The script works, because i've tested with other site in JSON and works perfectly. 该脚本有效,因为我已经在JSON中与其他站点进行了测试,并且可以完美运行。 I've already put this in web.config : 我已经把它放在web.config中

<httpProtocol>
       <customHeaders>
         <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
 </httpProtocol>

And the same problem, could you guys help me with this? 同样的问题,你们可以帮我吗?


I've get the fix Well, after try some things and @mattferderer opened my mind, i figured out what's going on with all this... 我已经解决了,好吧,尝试了一些事情之后,@ mattferderer打开了我的脑海,我弄清楚了所有这一切是怎么回事...

First i've removed the web.config side and leave only the header() function. 首先,我删除了web.config端,仅保留header()函数。 Okay first problem fixed (lol); 好,首先解决的问题(大声笑);

And than, i've figured out why $.getJSON() don't load the informations of other website... It's simple, just add (at least i did it in PHP ) this: 而且,我已经弄清楚了为什么$ .getJSON()不加载其他网站的信息...很简单,只需添加(至少我在PHP中做到了):

header('Content-Type: text/javascript');

In my case, the page is in format text/html , so $.getJSON() will not able to load all informations in that page! 就我而言,该页面的格式为text / html ,因此$ .getJSON()将无法加载该页面中的所有信息!

Thanks guys and sorry for the terrible english! 谢谢大家,对不起英语不好!

Try removing the access-control-allow-origin from your web.config since the problem is the access-control-allow-origin is being used twice. 尝试从您的web.config中删除access-control-allow-origin,因为问题是两次使用了access-control-allow-origin。 If you look in your Response Headers it shows this: 如果您在响应标题中查找,则会显示以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM