页面未显示JSON对象属性

时间:2016-11-06 06:25:49

标签: javascript jquery json

我的代码没有在控制台中播放对象属性,但代码显示对象就好了。我无法访问此对象中的信息?

这是我的代码:

//                 APOD
(function Apod() {
    var api_key = 'NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo';
    var url = 'https://api.nasa.gov/planetary/apod' + "?api_key=" + api_key;
    var apodRequest = new XMLHttpRequest();
    var apodDATA = "";
    apodRequest.onreadystatechange = function() {
        apodRequest.onload = function() {
            var responseObject = apodRequest.response;
            apodDATA = responseObject;
            $("document").ready(function() {
                $("#apodimage").attr("src", responseObject.hdurl);
            });
            console.log(responseObject.url);
        };
}
    apodRequest.open("GET", url, true);
    apodRequest.send(null);
}());

这是JSON"对象"在responseObject变量上显示正常(属性给我未定义):

{
  "date": "2016-11-06",
  "explanation": "A mere 20,000 light-years from the Sun lies NGC 3603, a resident of the nearby Carina spiral arm of our Milky Way Galaxy. NGC 3603 is well known to astronomers as one of the Milky Way's largest star-forming regions. The central open star cluster contains thousands of stars more massive than our Sun, stars that likely formed only one or two million years ago in a single burst of star formation. In fact, nearby NGC 3603 is thought to contain a convenient example of the massive star clusters that populate much more distant starburst galaxies. Surrounding the cluster are natal clouds of glowing interstellar gas and obscuring dust, sculpted by energetic stellar radiation and winds. Recorded by the Hubble Space Telescope, the image spans about 17 light-years.   Follow APOD on: Facebook,  Google Plus,  Instagram, or Twitter",
  "hdurl": "http://apod.nasa.gov/apod/image/1611/ngc3603_hubble_3885.jpg",
  "media_type": "image",
  "service_version": "v1",
  "title": "Starburst Cluster in NGC 3603",
  "url": "http://apod.nasa.gov/apod/image/1611/ngc3603_hubble_960.jpg"
} 

2 个答案:

答案 0 :(得分:2)

从服务器获得的内容可能只是一个字符串,而不是一个对象。

您可以使用JSON.parse解析JSON字符串并将其转换为对象。

var obj = JSON.parse(responseObject);
console.log(obj.url);

您可以使用typeof检查变量的类型。因此,如果您打印console.log(typeof responseObject),您将获得"string"。如果它是一个对象,你会得到"object"

此外,由于您已经在使用jQuery,请考虑通过jQuery本身执行ajax请求。它会更优雅。阅读文档here

答案 1 :(得分:1)

USE JSON.parse将您的回复转换为json,因为您的请求正在返回string
注意: - 请勿在ajax响应中使用$("document").ready()

它的工作正常我

(function Apod() {
    var api_key = 'NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo';
    var url = 'https://api.nasa.gov/planetary/apod' + "?api_key=" + api_key;
    var apodRequest = new XMLHttpRequest();
    var apodDATA = "";
    apodRequest.onreadystatechange = function() {
        apodRequest.onload = function() {
            var responseObject = apodRequest.response;
            apodDATA = responseObject;
            $("#apodimage").attr("src", responseObject.hdurl);
         var json =  JSON.parse(responseObject);
            console.log(json.url);
        };
}
    apodRequest.open("GET", url, true);
    apodRequest.send(null);
}());