在代码中找不到语法错误

时间:2016-04-27 05:31:16

标签: javascript jquery ajax xml

Chrome开发工具告诉我第3行有错误,但我不确定它是什么。不可否认,我不熟悉使用jQuery进行编码,因此我所遵循的教程可能出错了。

$.ajax({
    url: 'https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml',
    type: "Get",
    dataType: 'xml',
    success: function (result) {
        }
        $(result).find('Module').each(function() {
            //$("#ModsList").append($(this).text());
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var episode = $(this).find('episode').text();
            $("#ModsList").append("<tr>" + "<td>" + $authors + "</td>" + "<td>" + $version + "</td>" + "<td>" + $date + "</td>" + "<td>" + $episode + "</td>" + "</tr>");
        });
    error: function() {
    alert("Notify the site owner that the xml file has a syntax error and is therefore unreadable.");
    }
});

这是我试图通过以上代码修改的表格:

<table id="ModsList">

    <tr style="font-weight: bold;">

         <td>Mod Name</td>

         <td>Author(s)</td>

         <td>Version</td>

         <td>Date added/updated</td>

         <td>Episode Added</td>

    </tr>

</table>

3 个答案:

答案 0 :(得分:1)

您的success处理程序未正确声明。您必须将代码放在成功函数的{ }之间。正如您现在所做的那样,您将随机代码插入到对象定义中,这是不合法的。

将代码更改为:

$.ajax({
    url: 'https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml',
    type: "Get",
    dataType: 'xml',
    success: function (result) {
        $(result).find('Module').each(function() {
            //$("#ModsList").append($(this).text());
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var episode = $(this).find('episode').text();
            $("#ModsList").append("<tr>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>"+episode+"</td>" + "</tr>");
        });
    },
    error: function() {
        alert("Notify the site owner that the xml file has a syntax error and is therefore unreadable.");
    }
});

答案 1 :(得分:0)

试试这个

 $.ajax({
    url: 'https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml',
    type: "Get",
    dataType: 'xml',
    success: function (result) {

        $(result).find('Module').each(function() {
            //$("#ModsList").append($(this).text());
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var episode = $(this).find('episode').text();
            $("#ModsList").append("<tr>" + "<td>" +authors+ "</td>" + "<td>" +version+ "</td>" + "<td>" +date +"</td>" + "<td>" +episode+ "</td>" + "</tr>");
        });
},
    failure: function() {
    alert("Notify the site owner that the xml file has a syntax error and is therefore unreadable.");
    }
});

答案 2 :(得分:0)

为什么成功:function(result){}是空的?希望结果只有在成功函数下才能访问。

相关问题