繁体   English   中英

Javascript未定义错误(在HTML页面中加载JSONP)

[英]Javascript undefined error (loading JSONP in HTML page)

我有一个关于将JSONP加载到HTML中的问题。

这是我的JSONP文件:

myCallback({
"news": {
    "item": [
        {
            "Title": "IceBridge Preparations Continue",
            "Author": "George Hale, IceBridge Science Outreach Coordinator",
            "Date": "14/09/2012",
            "Intro": "The work of installing IceBridge's science instruments on the NASA DC-8 airborne laboratory continued this week.",
            "Content": "People from the Center for the Remote Sensing of Ice Sheets at the University of Kansas (CReSIS) and from Sander Geophysics Limited (SGL) spent the week installing the aircraft's various radar instruments and the AirGrav gravimeter. With the last of the instruments installed and operational, IceBridge is now ready to start test flights next week. Monday afternoon's schedule includes pilot proficiency flights and on Tuesday and Wednesday IceBridge will carry out instrument check flights."
        },
        {
            "Title": "Preparing the DC-8 for Antarctica 2012",
            "Author": "George Hale, IceBridge Science Outreach Coordinator",
            "Date": "20/09/2012",
            "Intro": "Over the next few weeks the IceBridge team will prepare NASA's DC-8 airborne laboratory for the 2012 Antarctic campaign.",
            "Content": "Long hours in the hangar at NASA's Dryden Flight Research Facility mean that the MCoRDS antenna and Airborne Topographic Mapper have been installed and all ground tests for ATM are complete. Next week, the radar and gravimeter teams will begin their preparation work."
        },
        {
            "Title": "Q&A: Michael Studinger",
            "Author": "Maria-Jose Viñas, Cryospheric Sciences Laboratory Outreach Coordinator",
            "Date": "30/09/2012",
            "Intro": "Michael Studinger is Operation IceBridge’s project scientist.",
            "Content": "He trained as a geophysicist in Germany, his home country, before moving to the U.S. to take a position at the Lamont-Doherty Earth Observatory and then transferring to NASA Goddard Space Flight Center in 2010. Studinger has been studying polar regions for 18 years, expanding his initial focus on the geology and tectonics of the Antarctic continent to the overall dynamic of polar ice sheets."
        }
    ]
}

})

现在我想在HTML页面中加载它。

我用Javascript做到这一点:

<script type="text/javascript">

function myCallback(data)
{
    var htmlContent;
    $.each(data.news.item,function(index, item)
    {
    htmlContent += "<article>" + "<h1>" + item.Title + "</h1>";
    htmlContent += "<h2>" + item.Author + "</h2>";
    htmlContent += "<p><i>" + item.Date + "</i></p>";
    htmlContent += "<p>" + item.Intro + "</p>";
    htmlContent += "<p>" + item.Content + "</p>" + "</article>";
    });
    $("#main").html(htmlContent);
}

在身体我有<div id="main"></div>

这一切都是正确的,但我总是在html页面的顶部和我的json文件中的文本未定义。 谁知道我怎么解决这个问题?

这个:

var htmlContent;

应该是这样的:

var htmlContent = '';

没有值的声明变量是undefined 发生的事情是,当你这样做时:

htmlContent += "<article>";

你基本上是在做htmlContent = undefined + "<article>" JS将undefined强制转换为字符串以将其添加到另一个字符串。

将变量初始化为空字符串可防止该表单发生。

我不确定这是否会解决它但你可能需要首先初始化你的htmlContent字符串htmlContent =""; 在追加它之前。

暂无
暂无

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

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