2016-04-27 60 views
0

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> 
+1

哪一行是3?當你只有'authors'時爲什麼要使用'$ authors'? – guradio

+0

請發佈您正在收到的錯誤 –

+0

您的成功回調被錯誤地關閉。 – hmd

回答

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

爲什麼成功:函數(結果){}正在空的?希望結果只能在成功函數中訪問。