【问题标题】:xml parser not working?xml解析器不工作?
【发布时间】:2011-12-14 15:11:29
【问题描述】:

我编写了以下代码来解析和显示一个 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"); 
            } 

};

【问题讨论】:

  • http://fb.mobilechilli.com 和请求它的页面是同一个域吗?
  • 不,它是一个不同的域 - 这是一个问题吗?
  • 是的。跨域请求通常仅限于jsonp 数据类型。

标签: jquery mobile xml-parsing


【解决方案1】:

你正在做一个 POST,但为什么呢?您需要检索 XML 内容,对吗?然后使用 GET 并确保您从同一个域执行 ajax 查询。

如果您不能更改域,那么您必须使用代理 php 文件来为您获取所需文件的内容,在同一个域上,您执行 jquery ajax 调用。

Cross domain ajax query

此外,如果您使用 errorMsg 指定以下参数,您可以检查异常情况:

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

【讨论】:

    【解决方案2】:

    您必须使用 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 是代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多