【发布时间】: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);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
【问题讨论】:
-
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