The following works fine in Chrome and Firefox but is not working in IE. It is getting data in IE (I can see it in the console) - but it's not looping through either .each
:
下面的代码在Chrome和Firefox中运行良好,但在IE中不工作。它在IE中获取数据(我可以在控制台看到)——但是它也没有通过任何一个循环。
function searchServing(which,choice){
var url = "search.asp?" + which + "=" + choice;
$.get(url, function(data){
console.log("data" + data);
$.each($(data).find("company"),function(index, el) {
loc = $(this).find('Location').text();
console.log("loc: "+ loc);
});
})//$.get(url, function(data){
.error(function() {
})//.error(function() {
.success(function(data) {
$.each($('company',data),function(index, el) {
loc = $(this).find('Location').text();
console.log("location: "+ loc);
}
}
}
XML is in this format:
XML是这种格式:
<company>
<sql><%=sSQL%></sql>
<uid><%=uid%></uid>
<companyName><%=company%></companyName>
<location><%=location%></location>
<phone1><%=phone1%></phone1>
<phone2><%=phone2%></phone2>
<phone3><%=tollfree%></phone3>
<serving><%=serving%></serving>
</company>
2 个解决方案
#1
2
Might be because of console.log()
可能是因为console.log()
As IE 8
and under has no console object by default
默认情况下,IE 8和under没有控制台对象。
You need to open the developer tools to make it work or create a new console object.
您需要打开开发人员工具以使其工作,或者创建一个新的控制台对象。
#2
3
Firstly, IE does'nt always like console.log, so remove that.
首先,我不总是喜欢控制台。日志,所以删除。
Secondly, if the tagname is in lowercase, use lowercase letters when trying to find it.
其次,如果标记名是小写的,在查找时使用小写字母。
thirdly, use the var
keyword, and don't declare variables as globals inside a loop.
第三,使用var关键字,不要将变量声明为循环中的全局变量。
Last but not least, XML should be parsed with $.parseXML
, like so:
最后但并非最不重要的是,应该用$来解析XML。parseXML,像这样:
function searchServing(which, choice) {
var url = "search.asp?" + which + "=" + choice,
loc = []; //use array if iterating several values
$.get(url, function(xml) {
var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);
$.each($xml.find("company"), function(index, el) {
loc.push($(el).find('location').text());
});
});
}
#1
2
Might be because of console.log()
可能是因为console.log()
As IE 8
and under has no console object by default
默认情况下,IE 8和under没有控制台对象。
You need to open the developer tools to make it work or create a new console object.
您需要打开开发人员工具以使其工作,或者创建一个新的控制台对象。
#2
3
Firstly, IE does'nt always like console.log, so remove that.
首先,我不总是喜欢控制台。日志,所以删除。
Secondly, if the tagname is in lowercase, use lowercase letters when trying to find it.
其次,如果标记名是小写的,在查找时使用小写字母。
thirdly, use the var
keyword, and don't declare variables as globals inside a loop.
第三,使用var关键字,不要将变量声明为循环中的全局变量。
Last but not least, XML should be parsed with $.parseXML
, like so:
最后但并非最不重要的是,应该用$来解析XML。parseXML,像这样:
function searchServing(which, choice) {
var url = "search.asp?" + which + "=" + choice,
loc = []; //use array if iterating several values
$.get(url, function(xml) {
var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);
$.each($xml.find("company"), function(index, el) {
loc.push($(el).find('location').text());
});
});
}