作者:郭洁蓉4071_878 | 来源:互联网 | 2023-06-03 13:51
Imagineyouhaveadata.xmlfile:想象一下,你有一个data.xml文件:<?xmlversion1.0encodingUTF-8?>
Imagine you have a data.xml file:
想象一下,你有一个data.xml文件:
- value1
- value2
- value3
I'm trying to store all the data config into a single variable in order to use it in my .js code using this:
我正在尝试将所有数据配置存储到单个变量中,以便在我的.js代码中使用它:
$(document).ready(function() {
'use strict';
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'xml',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
results = $.getValues("data.xml");
console.log(results);
});
If I refresh the page I get in results variable a Document object with fields like URL, baseURI, body ...
如果我刷新页面,我会在结果变量中找到一个Document对象,其中包含URL,baseURI,body等字段...
If I refresh again I get in results a #document object with the data from the data.xml:
如果我再次刷新,我会使用data.xml中的数据获得#document对象的结果:
- value1
- value2
- value3
So the type of object returned changes each time someone goes to the url.
因此,每当有人访问url时,返回的对象类型都会发生变化。
I have two questions about this:
我有两个问题:
How can I make the returned value always #document? (which contains the data from the .xml file)
如何使返回值始终为#document? (包含.xml文件中的数据)
How can I access to an element from #document?
如何从#document访问元素?
I have tried using:
我试过用过:
console.log(results.root.item);
console.log(results.find("item"));
But both give me errors.
但两者都给我错误。
Maybe there is a better way to do this (meaning reading xml data into a single variable).
也许有更好的方法(意味着将xml数据读入单个变量)。
Any suggestions?
有什么建议么?
2 个解决方案