2013-08-21 32 views
0

我試圖從文件'forum.xml'中描述的線程列表中讀取。我意識到我的GET請求沒有成功。這裏是XML文件(不可修改)jQuery GET未成功

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE forum SYSTEM "forum.dtd"> 
<forum> 
<thread> 
    <title>Tea Party</title> 
    <posts>teaParty.xml</posts> 
</thread> 
<thread> 
    <title>COMP212 Exam</title> 
    <posts>crypto.xml</posts> 
</thread> 
</forum> 

這裏是我的js。我已經測試了目標元素正在被選中。

//threadReader.js 
//Gets and display list of threads 


var Threads = (function() { 
var pub = {}; 
var target = $(".thread"); 
var xmlSource = 'forum.xml'; 

function showThreads() { 
    console.log("showThreads called"); 
    console.log(xmlSource); 
    $({ 
     type: "GET", 
     url: xmlSource, 
     cache: false, 
     success: function(data) { 
      console.log(data); 
      parseThreads(data, target); 
     } 
    }); 
} 

function parseThreads(data, target) { 
    console.log("parseThreads called"); 
    console.log(target); 
    console.log(data); 

    target.append("<ul>"); 
    $(data).find("title").each(function() { 
     $(target).append("<li>"); 
     $(target).append($(this).text()); 
     $(target).append("</li>"); 
    }); 
} 

pub.setup = function() { 
    showThreads(); 
} 

return pub; 
}()); 

$(document).ready(Threads.setup); 

任何見解總是讚賞

回答

2

更改此

function showThreads() { 
    console.log("showThreads called"); 
    console.log(xmlSource); 
    $({ 

function showThreads() { 
    console.log("showThreads called"); 
    console.log(xmlSource); 
    $.ajax({ 

另外要注意,你要$(".thread")通話可能不符合當時的任何元素你我們在呼喚它。最好在你的文檔中準備好處理程序。

0

這可能有助於未來。爲了得到正確的Jquery Ajax的語法

http://api.jquery.com/jQuery.ajax/

在你的情況,我想這應該解僱呼叫。

function showThreads() { 
    console.log("showThreads called"); 
    console.log(xmlSource); 
    $.ajax({ 
     type: "GET", 
     url: xmlSource, 
     cache: false, 
     success: function(data) { 
      console.log(data); 
      parseThreads(data, target); 
     } 
    }); 
}