beginner
beginner

Reputation: 2508

How to fetch data from xml using ajax

I had a file data.php

<div class="content">hello</div>

I can fetch data in main file using

main.php

<div id="content1"></div>
<script>
$.post("data.php",function(r,s){$("#content1").html(r);});
</script>

This script fetches the data from data.php even if i try data.txt it also works but when i convert data.php into data.xml this does not work

<div id="content1"></div>
<script>
$.post("data.xml",function(r,s){$("#content1").html(r);});
</script>

what should i do to fetch data from xml file. please help

Upvotes: 0

Views: 3135

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 340

If you want to use jquery ajax then try this method.

  $.ajax({
                type: "POST",
                url: "data.xml",
                dataType: "xml",
                success: function (xml) {
                    var xmlDoc = $.parseXML(xml),
                    $xml = $(xmlDoc);
                    $title = $xml.find( "title" );
                    $("#someElement" ).append( $title.text() );
                }
            });

You need to parseXml data.

Upvotes: 1

Related Questions