Dancer
Dancer

Reputation: 17671

xml parser not working?

I've written the following code to parse and display an xml file - works fine with dynamically created xml files - but for some bizzare reason it wont read local files or straightforward xml files off my server! Errors every time - cannot for the life of me work out why!

Any advice would be very welcome!

function testPay() {


$.ajax({
    type: "POST",
url: "http://fb.mobilechilli.com/chillifacebook.xml",
dataType: "xml",
error: errorMsg, 
success: function(mml) {

            alert("here");          
                $(mml).find("album").each(function() 
                    { 
                            var titleA = $(this).find('productDescription').text();
                                if(titleA.length > 13){
                                var title = titleA.substring(0,10) +"..";}
                                else if(titleA.length < 13){title = titleA; }
                            var artistA = $(this).find('artist').text();
                            if(artistA.length > 13){
                                var artist = artistA.substring(0,10)+ "..";
                                }
                                else if(artistA.length < 13){artist = artistA; }
                            var artwork = $(this).find('artwork').text(); 
                            var price = "Buy £" + $(this).find('price').text();
                            var mediaItem = $(this).find('mediaItem').text();
                            var artwork = $(this).find('artwork').text();
                            var chargeCode = $(this).find('chargecode').text();
                            var productCode = $(this).find('productCode').text();
                            var listItem = $('<div class="mediaBlock"><form action="https://wpg.dialogue.net/pfiwidget/ButtonHandler" method="post" id="pfi_form'+mediaItem+'" name="pfi_form" target="thisframe"><input type="hidden" name="transactionRequest" id="pfi_transactionRequest" value="' + chargeCode + '"><input type="hidden" name="productCode" id="pfi_productCode" value="' +productCode+'"><input type="hidden" name="productDescription" id="pfi_productDescription" value="'+title+'"><input type="hidden" name="category" id="pfi_category" value="MUSIC"><input name="notificationUrl" type="hidden" value="http://fb.mobilechilli.com/chilli_shop/index-shop_xml.php" /><input type="hidden" value="http://www.facebook.com/pages/Chilli-Music/145522908841649/?affiliate=facebook_order" name="fulfilmentUrl"><input type="hidden" value="http://www.facebook.com/ChilliMusicStore?v=app_152948274779752" name="returnUrl"><input type="hidden" name="serviceDeliveryMessage" id="pfi_serviceDeliveryMessage" value="https://www.mobilechilli.com/mobile-downloads-uk/thanks.php"><input type="hidden" value="chilli_GB_Facebook" name="brandName"><div class="promoImg floL"><a name="pfi_form'+mediaItem+'" class="overlayLink" href="#data"><img src="'+artwork+'" width="82" height="85" alt="'+artist+'-'+title+' " /></a><div class="promoContent"><h2 class="red">'+artist+'</h2><h2>'+title+'</h2><div class="buyBtn"><span><a  name="pfi_form'+mediaItem+'" class="overlayLink" href="#data">'+price+'</a> </div></div></div></form>');                               
                            $(".trackRow").append(listItem);
                            hideLoading();
                    });

}});


function errorMsg() { 
                    alert("error getting xml feed"); 
            } 

};

Upvotes: 0

Views: 473

Answers (2)

kubetz
kubetz

Reputation: 8566

You must use JSONP to access data crossdomain. One trick to convert this XML into JSONP and parse it is using YQL service.

$.ajax({
  url: 'http://query.yahooapis.com/v1/public/yql',
  data: { 
    q: 'select * from xml where url="http://fb.mobilechilli.com/chillifacebook.xml"',
    format: 'json'
  },
  dataType: 'JSONP',
  success: function(data) {
    // this is the XML in JSON format - explore the object in the console
    console.log(data.query.results);

    // example - display a list of artist names
    var liArtists = $.map(data.query.results.catalogueResultSet.catalogueResult, function(res) {
      return '<li>' + res.artist + '</li>';
    });

    $('<ul />', { html: liArtists.join('') }).appendTo('body');
  }
});

HERE is the code.

Upvotes: 0

Jauzsika
Jauzsika

Reputation: 3251

You are doing a POST, but why? You need to retrieve the XML contents, right? Then use GET and make sure you are doing the ajax query from the same domain.

If you can't change the domain, then you have to use a proxy php file which gets the wanted file's content for you, on the same domain, you doing the jquery ajax calls.

Cross domain ajax query

Also, you can check what's the exception if you specify the following arguments with errorMsg:

function errorMsg(xhr, ajaxOptions, thrownError)
{
    console.log(thrownError);
}

Upvotes: 2

Related Questions