Ajax调用成功,但有一个未定义的问题

时间:2014-06-18 06:07:36

标签: javascript jquery html ajax

请查看Fiddle Example

任何人都可以告诉我为什么有一个" undefined"即使Ajax请求成功完成,附加到div元素#tablearea的文本?

JSON:

[{"title":"A","nation":"Germany,Japan","city":"Hamburg,Toyko","Name":"John,Peter,Tom"},{"title":"B","nation":"Japan,Italy","city":"Toyko","Name":"Adam,Tom"},{"title":"C","nation":"Germany","city":"Berlin,Hamburg","Name":"Mary,Tom"}]

jQuery的:

   $.ajax({
        url: "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%22http%3A%2F%2Fgoo.gl%2FRgDyl4%22&format=json&diagnostics=true&callback=",
        success: function (data) {
            var item_html;
            item_html += "<table><thead><tr><th></th>";            
            $(data.query.results.json.json).each(function (index, item) {
                var title = item.title;
                item_html += '<th>'+title+'</th>';
            });
            item_html += "</tr></thead><tbody><tr><td>Germany</td>";
            $(data.query.results.json.json).each(function (index, item) {
                var nations = item.nation.replace(/ /g,'').split(",");
                console.log(nations);
                if ($.inArray('Germany', nations) < 0)
                {
                  item_html += "<td>No</td>";
                }else{
                  item_html += "<td>Yes</td>";
                }
                 });    

             item_html += "</tr><td>Berlin</td>";

             $(data.query.results.json.json).each(function (index, item) {
                var  citys = item.city.replace(/ /g,'').split(",");
                if ($.inArray('Berlin', citys) < 0)
                {
                  item_html += "<td>No</td>";
                }else
                {
                  item_html += "<td>Yes</td>";
                }

             });

              item_html += "</tr><td>Peter</td>";

             $(data.query.results.json.json).each(function (index, item) {
                var  names = item.Name.replace(/ /g,'').split(",");
                if ($.inArray('Peter', names) < 0)
                {
                  item_html += "<td>No</td>";
                }else
                {
                  item_html += "<td>Yes</td>";
                }

             });

           item_html += "</tr></tbody></table>";

           $('#tablearea').append(item_html);

        }
    });

HTML:

<div id="tablearea"></div>

返回结果:

<div id="tablearea">    
undefined
<table>
<thead>
<tr>
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td>Germany</td>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Berlin</td>
<td>No</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Peter</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
</div>

3 个答案:

答案 0 :(得分:2)

您必须首先使用item_html初始化"",如下所示:

item_html = "";

如果你只是声明item_html而没有初始化它,那么它是未定义的。在其上附加一个字符串会导致代码的行为。

有关详细说明,请参阅此fiddle

答案 1 :(得分:1)

尝试使用值

声明item_html
var item_html ="";

答案 2 :(得分:0)

您还可以完全删除以下语句。

var item_html;

这是一个糟糕的程序,但如果你愿意,你仍然可以这样做。