Keertan
Keertan

Reputation: 113

Ajax xml not going into success

I am trying to access the book.xml file which is in same folder as other files, everything is perfect, but the ajax function wont go into success. [object object] error is being showed. its a very simple xml. I generally do it via a php file. but i have to do it directly this time. i used dataType = "xml". seems to be a problem in that may be. anyway, please help

the html code:

<body>

<h2>Hello there, ajax example is loading:</h2>
<div class="row container">

<button class="btn" onclick="gettingdata();">Get data from XML</button>

<hr>

<div class="data">

<table id="datatable">
<tbody>
  <tr>
    <th>Book name</th>
    <th>Author</th>
    <th>year</th>
    <th>price</th>
  </tr>

  </tbody>
</table>

</div>


</div>

</body>

the js code:

$.ajax({

            type: 'GET',
            url: 'books.xml',
            dataType: 'xml',
            success: function(result) {
                alert("into");

                $(result).find('book').each(function() {

                    $('.datatable tbody').append(
                        '<tr>' +
                            '<td>' +
                                $(this).find('title').text() + '</td> ' +
                            '<td>' +
                                $(this).find('author').text() + '</td> ' +
                            '<td>' +
                                $(this).find('year').text() + '</td> ' +
                            '<td>' +    
                                $(this).find('price').text() + '</td> ' +   
                        '</tr>');
                });
            },
            error: function (textStatus, errorThrown) {
                alert(''+textStatus+errorThrown);
            },
            complete: function(){
                alert("done");
            }
        });

Upvotes: 0

Views: 65

Answers (2)

Keertan
Keertan

Reputation: 113

this is the xml:

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>

Upvotes: 0

Abhijeet
Abhijeet

Reputation: 4309

(Reason for your ajax call fail)Your code is working fine just put your books.xml on correct location . Also data in xml file should be valid.

And change

 $('.datatable tbody').append

to

 $('#datatable tbody').append

I tried your code on my system. It was working fine. Just make above changes.

Upvotes: 1

Related Questions