Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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访问NASA api_Javascript_Arrays_Json - Fatal编程技术网

使用Javascript访问NASA api

使用Javascript访问NASA api,javascript,arrays,json,Javascript,Arrays,Json,所以我对使用Javascript还不熟悉,我想知道如何访问NASA API,比如 使用这些数据呢?我想得到“日期”并将它们放入一个数组中,但我不知道怎么做。如何使用Javascript实现这一点 如果使用jQuery,可以使用jQuery.ajax(url)方法进行ajax调用,并将“”作为url传入 编辑:这是在jquery中执行ajax请求的更详细代码/说明: $.ajax({ // The URL for the request url: "https://api.nas

所以我对使用Javascript还不熟悉,我想知道如何访问NASA API,比如
使用这些数据呢?我想得到“日期”并将它们放入一个数组中,但我不知道怎么做。如何使用Javascript实现这一点

如果使用jQuery,可以使用jQuery.ajax(url)方法进行ajax调用,并将“”作为url传入

编辑:这是在jquery中执行ajax请求的更详细代码/说明:

$.ajax({

    // The URL for the request
    url: "https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY",


    // Whether this is a POST or GET request
    type: "GET",

    // The type of data we expect back
    dataType : "json",
})
  // Code to run if the request succeeds (is done);
  // The response is passed to the function
  .done(function( json ) {
     console.log(json)
  })
  // Code to run if the request fails; the raw request and
  // status codes are passed to the function
  .fail(function( xhr, status, errorThrown ) {
    alert( "Sorry, there was a problem!" );
    console.log( "Error: " + errorThrown );
    console.log( "Status: " + status );
    console.dir( xhr );
  })
  // Code to run regardless of success or failure;
  .always(function( xhr, status ) {
    alert( "The request is complete!" );
  });

如果您使用的是jQuery,则可以使用jQuery.ajax(url)方法执行ajax调用,并将“”作为url传入

编辑:这是在jquery中执行ajax请求的更详细代码/说明:

$.ajax({

    // The URL for the request
    url: "https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY",


    // Whether this is a POST or GET request
    type: "GET",

    // The type of data we expect back
    dataType : "json",
})
  // Code to run if the request succeeds (is done);
  // The response is passed to the function
  .done(function( json ) {
     console.log(json)
  })
  // Code to run if the request fails; the raw request and
  // status codes are passed to the function
  .fail(function( xhr, status, errorThrown ) {
    alert( "Sorry, there was a problem!" );
    console.log( "Error: " + errorThrown );
    console.log( "Status: " + status );
    console.dir( xhr );
  })
  // Code to run regardless of success or failure;
  .always(function( xhr, status ) {
    alert( "The request is complete!" );
  });
了解Ajax。然后,您可以使用解析返回的数据,然后从结果数组中的每个项目获取日期。请参见下面的示例。如果您想使用像这样的库,那么AJAX代码将被简化

var url='1〕https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_键=演示_键';
var-httpRequest//在这里申报好范围
如果(window.XMLHttpRequest){//Mozilla、Safari、IE7+。。。
httpRequest=新的XMLHttpRequest();
}else如果(window.ActiveXObject){//IE 6及更早版本
httpRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
httpRequest.onreadystatechange=函数(){
if(httpRequest.readyState==XMLHttpRequest.DONE&&httpRequest.status==200){
returnedData=httpRequest.responseText;
var data=JSON.parse(returnedData);
if(data.hasOwnProperty('results')){
var dates=data.results.map(函数(结果){
返回结果.date;
});
console.log('日期:',日期);
}
}否则{
//仍然未准备就绪或发生错误
}
};
httpRequest.open('GET',url,true);
httpRequest.send(空)了解Ajax。然后,您可以使用解析返回的数据,然后从结果数组中的每个项目获取日期。请参见下面的示例。如果您想使用像这样的库,那么AJAX代码将被简化

var url='1〕https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_键=演示_键';
var-httpRequest//在这里申报好范围
如果(window.XMLHttpRequest){//Mozilla、Safari、IE7+。。。
httpRequest=新的XMLHttpRequest();
}else如果(window.ActiveXObject){//IE 6及更早版本
httpRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
httpRequest.onreadystatechange=函数(){
if(httpRequest.readyState==XMLHttpRequest.DONE&&httpRequest.status==200){
returnedData=httpRequest.responseText;
var data=JSON.parse(returnedData);
if(data.hasOwnProperty('results')){
var dates=data.results.map(函数(结果){
返回结果.date;
});
console.log('日期:',日期);
}
}否则{
//仍然未准备就绪或发生错误
}
};
httpRequest.open('GET',url,true);

httpRequest.send(空)在jquery中尝试这样使用它:

var array = [];
$.get('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY',function(data){
       for(var i=0; i< data.results.length; i++) {
        array.push(data.results[i].date);
       }
    console.log(array);
});
var数组=[];
$.get('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_key',函数(数据){
对于(var i=0;i

然后在控制台中可以看到结果。或者根据您的需要使用array变量

尝试使用jquery:

var array = [];
$.get('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY',function(data){
       for(var i=0; i< data.results.length; i++) {
        array.push(data.results[i].date);
       }
    console.log(array);
});
var数组=[];
$.get('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_key',函数(数据){
对于(var i=0;i

然后在控制台中可以看到结果。或者根据您的需要使用数组变量

中的代码可能会有帮助中的代码可能会有助于您更好地理解这段代码所做的事情,如果您想使用现代模式编写干净的异步代码,您应该尝试理解和使用它。回调/承诺是必要的,因为您不知道http请求返回数据需要多长时间。它们是一种表达“收到回复后立即执行”的方式。感谢@Skylar提供的资源-尽管需要注意的是,某些浏览器(主要是IE)不支持本机承诺(请参阅本节),因此如果必须支持使用任何旧浏览器的客户,他们应该相应地进行调整。我还建议您阅读本文(并完成练习),其中还提到了一位同事与我分享的异步代码和回调(主要是练习34-5)——如果您希望使用承诺而不增加jQuery的所有开销,并且看起来很有希望(抱歉,我不得不这么做)。。。我的意思是,它们看起来像是很好的选择,如果你想很好地理解这段代码,你会想理解它,如果你想用现代模式编写干净的异步代码,你应该试着去理解和使用它。回调/承诺是必要的,因为您不知道http请求返回数据需要多长时间。它们是一种表达“收到回复后立即执行”的方式。感谢@Skylar提供的资源-尽管需要注意的是,某些浏览器(主要是IE)不支持本机承诺(请参阅本节),因此如果必须支持使用任何旧浏览器的客户,他们应该相应地进行调整。我还建议您阅读本文(并完成练习),其中还提到了一位同事与我分享的异步代码和回调(主要是练习34-5)——如果您希望使用承诺而不增加jQuery的所有开销,并且看起来很有希望(抱歉,我不得不这么做)。。。我的意思是,它们看起来是不错的选择。我怎么能使用这些日期呢?像添加或减去两个日期一样,您可以使用index-array[i]访问数据,其中i是索引。或者在数组中搜索所需的值。我不知道你现在需要什么。也可以将数据添加到数组中-array.push(data)-它将添加到数组的末尾-我可以怎么做