简体   繁体   English

在jquery中成功调用ajax后,为什么不加载此函数? (更新)

[英]Why doesn't this function load after a successful ajax call in jquery? (updated)

I'm using the tutorial here , that used to work last month, but now it isn't. 我在这里使用的是上个月使用过的教程 ,但现在不是。 I've copied the relevant code below. 我已经在下面复制了相关代码。

<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>

The problem is that the xmlParser() function isn't being called after the successful ajax request. 问题是成功的ajax请求后没有调用xmlParser()函数。 It shows a 123 alert but not a 456 alert. 它显示123警报,但不显示456警报。 Am I doing something wrong, or is the tutorial wrong? 我做错了什么,还是本教程错了?

阿贾克斯好http 200

Previously on the other question I was told it was a CORS issue, but I was calling the file off my computer, and in my example, so what is the problem now? 以前,在另一个问题上,我被告知这是一个CORS问题,但是我是从计算机上调用该文件的,在我的示例中,那么现在出了什么问题?

if youre using 如果您使用

dataType: "xml"

and if you have an invalid xml in youre response then success wont triger 如果您的响应中包含无效的xml,则成功不会触发

check youre response because its invalid... if you want to test your code just change 检查您的响应,因为它无效...如果要测试代码,只需更改

dataType: "html" 

and you will see your alert(456) that confirms invalid xml data 并且您将看到确认无效xml数据的警报(456)

I've duplicated your issue and by placing in my response a valid xml, code was running fine 我已经复制了您的问题,并在响应中添加了有效的xml,代码运行良好

Also if youre expected data is just an string that contains an id, try using 另外,如果您期望的数据只是包含id的字符串,请尝试使用

dataType: "xml text"

NOTE: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require 注意:从jQuery 1.5开始,jQuery可以将dataType从Content-Type标头中接收到的数据类型转换为所需的数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM