【问题标题】:how to get data from child element to find other child element in xml api with jQuery?如何使用 jQuery 从子元素中获取数据以在 xml api 中查找其他子元素?
【发布时间】:2021-08-14 22:26:47
【问题描述】:

嘿,我是 jQuery 新手,所以我需要一点帮助。我正在尝试从特定手机型号获取特定信息,例如 cpu 和 ram 信息。我编写了 jQuery 代码,但我仍然无法渲染 ram 或 cpu信息,即使我已经获得了所有数据。

$(document).ready(function() {
    $.ajax({
      url: "https://shop.a1.net/is-bin/intershop.static/WFS/Mobilkom-A1Shop-Site/-/de_AT/resources/alle-handys-priv.xml?1622013976098",
      dataType:'xml',
      type:'GET',

      success: function(result) {
        console.log(result)

     
        $(result).find('item').each(function(){
         

        var product_id = $(this).find('productid').text();
      
         
            if( product_id === "bDUK92AjN5YAAAF2NotBee0o"){
            
             let my_product = $(this);
           

             
            // var ram = my_item.find('attributes').attr('ATTR_MAP_RAM').text();
             var ram = my_product.find('attributes').find('ATTR_MAP_RAM').text();
             var cpu = my_product.find('attributes').find('ATTR_MAP_CPU').text();

             var chipset = my_product.find('attributes').find('ATTR_MAP_CHIPSET').text();

           

           
             $('.panel').append(
                
                $('<li />',{
                    text:ram
                  
                }),
                $('<li />',{
                    text:cpu
                  
                })
                ,
                $('<li />',{
                    text:chipset
                  
                })
            )
            
            }
        });
       
        
      },
      error: function(error) {
        console.log(error);
      }
    });
   
  });
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

【问题讨论】:

  • let my_product = $(this); 产生了什么?如果您将其记录到控制台,它会按预期显示还是有错误?
  • 该 API 未启用 CORS,这会使您的 AJAX 请求失败。您需要通过后端对其进行路由。
  • 实际上,如果我控制台记录 my_product ,我将数据作为 m.fn.init [item, context: item] :( @ProfessorAbronsius
  • @ChrisG 好的,我怎么能解决这个问题?好像我从 api 获取数据?
  • 传递给错误回调的第一个参数是 jqXHR。这就是您要记录的内容。至少在堆栈 sn-p 中。

标签: javascript html jquery xml api


【解决方案1】:

我认为您的“ATTR_MAT_XXX”中有错字,请尝试使用:

...
             var ram = my_product.find('attributes').find('ATTR_MAT_RAM').text();
             var cpu = my_product.find('attributes').find('ATTR_MAT_CPU').text();
...

【讨论】:

  • 如果您确定,请不要发布答案,而是在评论中说出来并将问题标记为拼写错误。
猜你喜欢
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多