2011-12-14 120 views
0

我已經寫了下面的代碼來解析和顯示一個xml文件 - 與動態創建的xml文件一起工作正常 - 但對於一些bizzare原因,它不會讀取本地文件或直接從我的服務器上的xml文件!錯誤每一次 - 不能爲我的生活工作出爲什麼!xml解析器不工作?

任何意見將非常歡迎!

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"); 
      } 

}; 
+0

「http:// fb.mobilechilli.com」與請求它的頁面處於同一個域嗎? – 2011-12-14 15:18:39

回答

0

必須使用JSONP來訪問數據跨域。把這個XML轉換成JSONP並解析它的一個技巧是使用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是代碼。

2

你正在做一個POST,但爲什麼?你需要檢索XML內容,對吧?然後使用GET並確保您正在從同一個域執行ajax查詢。

如果您不能更改域名,那麼您必須使用代理php文件,它可以爲您在相同的域上獲取所需文件的內容,並執行jQuery Ajax調用。

Cross domain ajax query

此外,您還可以檢查有什麼異常,如果你與ERRORMSG指定以下參數:

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