![](/img/trans.png)
[英]Why doesn't this function load after a successful ajax call in jquery?
[英]Why doesn't this function load after a successful ajax call in jquery? (updated)
我在这里使用的是上个月使用过的教程 ,但现在不是。 我已经在下面复制了相关代码。
<script>
(function($) { //In case jQuery no conflict is being used
$(document).ready(function() { //wait for document ready
$("#loaddaold").click(function () {
alert("123");
//$( "#duplicateshere" ).empty();
$.ajax({
type: "GET",
url: "ajax-oldmessages.php",
dataType: "xml",
success: xmlParser
});
});
});
function xmlParser(xml) {
alert("456");
//$("#rev2").slideDown();
$(xml).find("result").each(function () {
$("#appendhere").append('<a href="/debates/' + $(this).find("id").text() + '">' + $(this).find("title").text() + '</a><br>');
//$(".book").fadeIn(1000);
});
}
})(jQuery);
// http://www.webdevdoor.com/jquery/javascript-delay-input-field-change/
// http://papermashup.com/parse-xml-with-jquery/
// (not used) http://www.paulund.co.uk/process-each-node-of-xml-in-jquery
</script>
问题是成功的ajax请求后没有调用xmlParser()函数。 它显示123警报,但不显示456警报。 我做错了什么,还是本教程错了?
以前,在另一个问题上,我被告知这是一个CORS问题,但是我是从计算机上调用该文件的,在我的示例中,那么现在出了什么问题?
如果您使用
dataType: "xml"
如果您的响应中包含无效的xml,则成功不会触发
检查您的响应,因为它无效...如果要测试代码,只需更改
dataType: "html"
并且您将看到确认无效xml数据的警报(456)
我已经复制了您的问题,并在响应中添加了有效的xml,代码运行良好
另外,如果您期望的数据只是包含id的字符串,请尝试使用
dataType: "xml text"
注意:从jQuery 1.5开始,jQuery可以将dataType从Content-Type标头中接收到的数据类型转换为所需的数据类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.