在代码中找不到语法错误

Can't find syntax error in code

本文关键字:语法 错误 找不到 代码      更新时间:2023-09-26

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>
您的

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.");
    }
});

为什么成功:函数(结果){} 为空?希望结果应该只能在成功功能上访问。

试试这个

 $.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.");
    }
});