Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用纯JS获取Json数组_Javascript_Arrays_Json_Jsp - Fatal编程技术网

Javascript 使用纯JS获取Json数组

Javascript 使用纯JS获取Json数组,javascript,arrays,json,jsp,Javascript,Arrays,Json,Jsp,我希望不使用JQuery就从跨域获取JSON数组。来自跨域的数据如下所示: { "transactionid": "LBS_48550801", "status": 0, "countrylist": [ { "id": 1, "name": "France", "latitude": 37.00039, "longitude": 35.31411, "extent": { "minlatitude": 36.53888499, "minlon

我希望不使用JQuery就从跨域获取JSON数组。来自跨域的数据如下所示:

{
      "transactionid": "LBS_48550801",
      "status": 0,
      "countrylist": 
    [
   { "id": 1, "name": "France", "latitude": 37.00039, "longitude": 35.31411, "extent": { "minlatitude": 36.53888499, "minlongitude": 34.76786904, "maxlatitude": 38.37851496, "maxlongitude": 36.40534884 }},
   { "id": 2, "name": "Germany", "latitude": 37.76454, "longitude": 38.27645, "extent": { "minlatitude": 37.40771898, "minlongitude": 37.43326512, "maxlatitude": 38.21203404, "maxlongitude": 39.24767592 } },
   //... .... .... 
   //... .... .... 
   //... .... .... 
   { "id": 98, "name": "Belgium", "latitude": 38.75754, "longitude": 30.5387, "extent": { "minlatitude": 37.78126803, "minlongitude": 29.68963308, "maxlatitude": 39.27915099, "maxlongitude": 31.71549708 } },
   { "id": 99, "name": "England", "latitude": 39.71812, "longitude": 43.03345, "extent": { "minlatitude": 38.96877501, "minlongitude": 42.28340184, "maxlatitude": 40.031208, "maxlongitude": 44.61092892 } },
    ]
}
<script type="application/javascript">
    var request = new XMLHttpRequest();
    request.open('GET', '/your/url', true);
    // this following line is needed to tell the server this is a ajax request
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    request.onload = function () {
    if (this.status >= 200 && this.status < 400) {
            var json = JSON.parse(this.response);
            // this is where you can do something with the json response
        }
    };
    request.send();
    });
</script>
在获得数据之后,我需要对其进行解析以获得“countrylist”列表

我试图解决这个问题,但示例都是JQuery。我不想用它。如您所见,我在输入中有transactionid和状态参数。因此,在导入数据之后,我需要对其进行解析以获得countrylist数组。我知道如何解析它,但我无法从服务中获取全部数据

我需要通过Javascript方法获取数据。服务器很好地支持检索数据

应采取的步骤是:

1-获取全部数据(这里是我卡住的地方)

2-将其解析为Json对象(我知道如何解析数据)


如何在不使用任何JQuery或其他准备好的库的情况下获取整个数据?

是否可以尝试将数据下载到隐藏的iframe,然后解析其文档的内容。body?

您可以使用XMLHttpRequest for ajax和JSON.parse for parse JSON:)

你不应该使用eval!Eval是邪恶的!你可以在网上看到

UPD:


如果没有CORS头,您可以使用JSONP。只需将
&callback=getJSONP
添加到您的URL。在JSFIDLE上尝试一下:

因为您的操作涉及跨域请求,所以我建议您使用jsonp请求


有你想要的。

在我的例子中,因为服务器需要一个
ajax
请求,或者它会给你一个404(防止),所以根据接受的答案,你需要额外一行来完成
getJSON
ajax版本,如下所示:

{
      "transactionid": "LBS_48550801",
      "status": 0,
      "countrylist": 
    [
   { "id": 1, "name": "France", "latitude": 37.00039, "longitude": 35.31411, "extent": { "minlatitude": 36.53888499, "minlongitude": 34.76786904, "maxlatitude": 38.37851496, "maxlongitude": 36.40534884 }},
   { "id": 2, "name": "Germany", "latitude": 37.76454, "longitude": 38.27645, "extent": { "minlatitude": 37.40771898, "minlongitude": 37.43326512, "maxlatitude": 38.21203404, "maxlongitude": 39.24767592 } },
   //... .... .... 
   //... .... .... 
   //... .... .... 
   { "id": 98, "name": "Belgium", "latitude": 38.75754, "longitude": 30.5387, "extent": { "minlatitude": 37.78126803, "minlongitude": 29.68963308, "maxlatitude": 39.27915099, "maxlongitude": 31.71549708 } },
   { "id": 99, "name": "England", "latitude": 39.71812, "longitude": 43.03345, "extent": { "minlatitude": 38.96877501, "minlongitude": 42.28340184, "maxlatitude": 40.031208, "maxlongitude": 44.61092892 } },
    ]
}
<script type="application/javascript">
    var request = new XMLHttpRequest();
    request.open('GET', '/your/url', true);
    // this following line is needed to tell the server this is a ajax request
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    request.onload = function () {
    if (this.status >= 200 && this.status < 400) {
            var json = JSON.parse(this.response);
            // this is where you can do something with the json response
        }
    };
    request.send();
    });
</script>

var request=new XMLHttpRequest();
open('GET','/your/url',true);
//需要下面这行代码来告诉服务器这是一个ajax请求
setRequestHeader(“X-request-With”,“XMLHttpRequest”);
request.onload=函数(){
如果(this.status>=200&&this.status<400){
var json=json.parse(this.response);
//在这里,您可以使用json响应做一些事情
}
};
request.send();
});

读取上述数据块不需要JSON,它是Javascriptdata@nrathaus如何导入数据?导入后,我想将其用作json对象。您的问题是数据还是跨域?@nrahaus我的问题是跨域。我不能仅使用Javascript从JSP获取全部数据。我可以做到这一点,但内容不仅仅是这个文件。服务器上有更多的内容,我需要得到服务器的响应。你能用eval()解析整个数据结构并丢弃你不需要的东西吗?如果我能只用Javascript得到整个数据,那么我当然可以解析它。问题是从跨域获取数据。非常抱歉,我无法理解您的问题:(我最初的想法是找到一些横向方法来获取所需的数据,例如通过编程方式将其src属性更改为交叉内容的URL,将其加载到隐藏的iframe中。如果不可能,请详细说明是什么阻止了您这样做,或者您需要从交叉内容中获取哪些其他信息。)domain@Jorge_B你不能阻止我们e iframe因为协议、域和端口必须匹配才能访问iframe.nope,所以它不能完全工作。请使用以下链接检查它:很遗憾,没有CORS头,因此您无法进行跨源请求。您可以在服务器应用程序中获取json并将其发送到浏览器。关于CORS,您可以在此处阅读不正确,我可以使用Java获取全部数据和资料。如果系统不支持这种技术,那么我也不会使用Java。因此,我需要使用xmlhttp请求获取json响应。@MarkKaran我已经用代码示例更新了我的答案。请在JSFIDLE上查看(结果在控制台中)亲爱的@kmakarychev,谢谢你的努力,但是我在JSFIDLE中也没有得到任何结果。这是我的错误吗?JSFIDLE可以工作吗?这不是完整的jsonp文档。它包含数组。我无法使用jsonp获得全部数据。