Javascript JSON在一个地址上工作,但在另一个地址上不工作

Javascript JSON在一个地址上工作,但在另一个地址上不工作,javascript,html,json,getjson,Javascript,Html,Json,Getjson,因此,我是一个完全的新手,我正在尝试找出如何正确使用json。基本上,我是在问为什么下面代码中的第一个按钮会检索并显示json,而第二个和第三个按钮不会。在浏览器中访问时,所有链接都以相同的格式显示,所以我看不出问题出在哪里 函数doJSON1(){ $.getJSON('http://time.jsontest.com/?alloworigin=true,函数(数据){ 警报(JSON.stringify(数据)) }); } 函数doJSON2(){ $.getJSON('http://

因此,我是一个完全的新手,我正在尝试找出如何正确使用json。基本上,我是在问为什么下面代码中的第一个按钮会检索并显示json,而第二个和第三个按钮不会。在浏览器中访问时,所有链接都以相同的格式显示,所以我看不出问题出在哪里


函数doJSON1(){
$.getJSON('http://time.jsontest.com/?alloworigin=true,函数(数据){
警报(JSON.stringify(数据))
});
}
函数doJSON2(){
$.getJSON('http://api.vircurex.com/api/get_info_for_1_currency.json?base=DOGE&alt=BTC,函数(数据){
警报(JSON.stringify(数据))
});
}
函数doJSON3(){
$.getJSON('http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132,函数(数据){
警报(JSON.stringify(数据))
});
}
获取JSON1
获取JSON
获取JSON2
获取JSON
获取JSON3
获取JSON

当javascript不工作时(大多数浏览器中为F12),检查控制台,第二和第三个链接分别抛出错误,如下所示:

XMLHttpRequest cannot load http://api.vircurex.com/api/get_info_for_1_currency.json?base=DOGE&alt=BTC. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
XMLHttpRequest cannot load http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. 
至于这意味着什么以及如何解决它,另一个stackoverflow答案可能比我更好地解释了它,以及所有精彩的链接资源

最终,如果承载资源(即您请求的文件)的服务器没有响应足够的头(Access Control Allow Origin就是其中之一),则您将无法访问另一个域中的资源


您确定第二个和第三个不是JSONP api(即,您将它们作为
标记而不是Ajax get包含),因为仅仅通过读取URL,看起来它们应该是api,但可能不符合CORS,因此JSONP。

这是一个跨域问题。大多数浏览器不允许跨域请求来防止XSS攻击等。存在允许此权限的标题


这可能是一个有用的资源:

我发现第二个链接搞乱了,确实让第三个链接正常工作了,我做了以下工作:

1-将主代码更改为:


函数doJSON1(){
$.getJSON('http://time.jsontest.com/?alloworigin=true,函数(数据){
警报(JSON.stringify(数据))
});
}
函数doJSON2(){
$.ajax({
键入:“GET”,
url:“/php.php”,
数据类型:“JSON”,
成功:功能(数据){
警报(JSON.stringify(数据));
}
});
}
函数doJSON3(){
$.getJSON('http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132,函数(数据){
警报(JSON.stringify(数据))
});
}
获取JSON1
获取JSON
获取JSON2
获取JSON
获取JSON3
获取JSON
2-添加此php文件以绕过“无访问控制源”:



控制台中有错误吗?嗯,我在第二个链接中得到了403…第二个链接给了我403XMLHttpRequest无法加载。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。(索引):无法加载1XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。(索引):1请解释这两个错误的含义。非常感谢您教我有关控制台的知识!现在,我们来看看为什么第三个链接不起作用。上面的两条错误线分别来自第二个和第三个链接
XMLHttpRequest cannot load http://api.vircurex.com/api/get_info_for_1_currency.json?base=DOGE&alt=BTC. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
XMLHttpRequest cannot load http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. 
<html>
<head> 
    <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>  
    <script>
        function doJSON1() {
            $.getJSON('http://time.jsontest.com/?alloworigin=true', function (data) {
                alert(JSON.stringify(data))
            });
        }
        function doJSON2() {
        $.ajax({          
            type:  'GET',
            url:   '/php.php',
            dataType: 'JSON',              
            success: function(data){
                alert(JSON.stringify(data));
            }
         });
        }
        function doJSON3() {
            $.getJSON('http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132', function (data) {
                alert(JSON.stringify(data))
            });
        }
</script> 
</head>
<body>         
    <h3>Get JSON1</h3>
    <button onclick="doJSON1()">Get JSON</button>
    <h3>Get JSON2</h3>
    <button onclick="doJSON2()">Get JSON</button>
    <h3>Get JSON3</h3>
    <button onclick="doJSON3()">Get JSON</button>
</body>
</html>    
<?php
header('Content-type: application/xml');
echo file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");
?>