desbest
desbest

Reputation: 4896

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. It shows a 123 alert but not a 456 alert. Am I doing something wrong, or is the tutorial wrong?

ajax okay 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?

Upvotes: 0

Views: 291

Answers (1)

Sebo Zoltan
Sebo Zoltan

Reputation: 203

if youre using

dataType: "xml"

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

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

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

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

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

Upvotes: 2

Related Questions