2011-12-14 126 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); 
}