jQuery GET not succeeding

jQuery GET not succeeding

本文关键字:succeeding not GET jQuery      更新时间:2023-09-26

我正在尝试读取文件"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);

任何见解都值得赞赏

更改此

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

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

还要注意,在调用$(".thread")时,对它的调用可能与任何元素都不匹配。最好在文档就绪处理程序中执行此操作。

这可能在将来有所帮助。要获得正确的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);
        }
    });
}