gpt4 book ai didi

javascript - XML 在 jquery 中的格式不正确

转载 作者:行者123 更新时间:2023-11-30 16:44:33 25 4
gpt4 key购买 nike

我调用了一个 ajax 函数,该函数从格式如下的 xml 文件中收集数据

<?xml version="1.0" encoding="utf-8"?>
<values>
<value date="2015-07-12">37.170</value>
<value date="2015-07-13">7.190</value>
<value date="2015-07-12">37.170</value>
<value date="2015-07-12">3.210</value>
<value date="2015-07-12">37.20</value>
</values>

当我从控制台收到响应时,它说 格式不正确:1:80

不确定这意味着什么,因为当我使用 xml 验证器时,他们都告诉我没有错误。任何帮助都会很棒。

        var data = [];
$.ajax({
url: 'test.xml', // name of file you want to parse
dataType: "xml", // type of file you are trying to read
success: parse, // name of the function to call upon success
async: false,
error: function(xhr, status, error) {
alert(xhr.responseText);
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
console.log("text status: " + textStatus);
console.log("error: " + err);
}
});

function parse(xml) {
var items = jQuery(xml).find("values");
var data = [];
var values = $(items).text();
$(document).find("Values").each(function () {
$(this).find("value").each(function () { data.push($this.text);});
});
console.log(data.length);
}

不确定如何将所有值放入数组中。

最佳答案

根据文档,您可能会尝试解析它两次:

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

因此我想这就是您要找的东西:

function parse(xml) {
//var items = jQuery(xml).find("values");
var items = xml.find("values");
var data = [];
var values = $(items).text();
$(document).find("Values").each(function () {
$(this).find("value").each(function () { data.push($this.text);});
});
console.log(data.length);
}

关于javascript - XML 在 jquery 中的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com