jQuery GET not succeeding
jQuery GET not succeeding
我正在尝试读取文件"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);
}
});
}
相关文章:
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jQuery document.ready not working
- Javascript/Jquery Blob not showing Chrome PDF
- ReferenceError: not defined
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- Javascript getElementsByTagName not working?
- jQuery's trim()的前缀为not运算符
- Javascript - element.childNodes does not see an append.newch
- SemanticUI模态not onDeny/onApprove事件未激发
- WebkitTransform not fluent
- JQuery.val( ) not working
- Tomcat websocket is not working
- :not选择器不适用于ul类-备选方案
- Javascript JSON.parse not working
- NodeJS Multer is not working
- React, Webpack: bundle.js is not generated
- jQuery GET not succeeding