为什么不是在jQuery的一个成功的Ajax调用后这个功能的负载? (更新)是在、负载、功能、Ajax

2023-09-10 21:55:22 作者:世人眼中一场迷

我使用这里的教程,所用上个月的工作,但现在还不是。我复制了相关的code以下。

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>

的问题是,所述XMLPARSER()函数不会被调用成功ajax请求之后。它显示了一个123的警报,但不是456警报。我做得不对,或者是教程错了吗?

The problem is that the xmlParser() function isn't being called after the successful ajax request. It shows a 123 alert but not a 456 alert. Am I doing something wrong, or is the tutorial wrong?

previously上的其他问题,我被告知这是一个CORS的问题,但我调用文件从我的电脑,并在我的例子,所以有什么问题呢?

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?

推荐答案

如果你使用的

dataType: "xml"

如果你有一个无效的XML在您选择的响应,那么成功不会TRIGER

and if you have an invalid xml in youre response then success wont triger

检查您选择的反应,因为其无效...如果你想测试你的code只需更改

check youre response because its invalid... if you want to test your code just change

dataType: "html" 

,您将看到您的警报(456),确认​​无效的XML数据的

and you will see your alert(456) that confirms invalid xml data

我重复您的问题,并通过将在我的回应是有效的XML,code的运行良好。

I've duplicated your issue and by placing in my response a valid xml, code was running fine

此外,如果您选择预期的数据仅仅是一个包含ID的字符串,尝试使用

Also if youre expected data is just an string that contains an id, try using

dataType: "xml text"

注:对于jQuery 1.5,jQuery的可转换数据类型从它在Content-Type头收到您所需要的

NOTE: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require

 
精彩推荐
图片推荐