首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript未定义错误(在HTML页面中加载JSONP )

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

Stack Overflow用户
提问于 2012-10-26 05:23:19
回答 2查看 405关注 0票数 1

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

这是我的JSONP文件:

代码语言:javascript
运行
复制
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做到了这一点:

代码语言: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文件中得到我的文本。有人知道我怎么解决这个问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-26 05:28:14

这一点:

代码语言:javascript
运行
复制
var htmlContent;

应该是这样:

代码语言:javascript
运行
复制
var htmlContent = '';

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

代码语言:javascript
运行
复制
htmlContent += "<article>";

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

将变量初始化为空字符串可防止这种情况的发生。

票数 3
EN

Stack Overflow用户

发布于 2012-10-26 05:29:35

我不确定这是否能解决这个问题,但是你可能需要先初始化你的htmlContent字符串,然后再追加到htmlContent ="";

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13077513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档