2011-12-14 21 views
0

私はxmlファイルを解析して表示するために以下のコードを書いています - 動的に作成されたxmlファイルでうまく動作します - しかし、いくつかの理由のためにローカルファイルや簡単な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」はリクエストしているページと同じドメインですか? –

+0

それは別のドメインではありません - これは問題ですか? – Dancer

+1

はい。クロスドメイン要求は、通常、 'jsonp'データ型に制限されます。 –

答えて

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クエリを実行していることを確認します。

ドメインを変更できない場合は、同じドメイン上でjquery ajaxを呼び出して、必要なファイルの内容を取得するプロキシphpファイルを使用する必要があります。

Cross domain ajax query

また、あなたがerrorMsg内容で次の引数を指定した場合は例外何を確認することができます。

function errorMsg(xhr, ajaxOptions, thrownError) 
{ 
    console.log(thrownError); 
} 
関連する問題