jQuery GET没有成功

时间:2013-08-21 07:10:45

标签: javascript jquery xml ajax get

我正在尝试从文件'forum.xml'中描述的线程列表中读取。我已经意识到我的GET请求没有成功。这是XML文件(不可修改)

<?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 个答案:

答案 0 :(得分:2)

更改此

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

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

另请注意,您拨打$(".thread")的电话可能与您拨打电话时的任何元素都不匹配。最好在文档就绪处理程序中执行此操作。

答案 1 :(得分: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);
        }
    });
}