jquery $.ajax()取xml數(shù)據(jù)的小問題解決方法
更新時間:2010年11月20日 20:32:13 作者:
今天想做一個用$.ajax()從xml中讀取數(shù)據(jù)的這么一個異步交互過程
開始的代碼如下:
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "xml",
timeout: 2000,
beforeSend: function () {},
success: function (xml) {
$(xml).find("app[id='id-1']").find("auther").appendTo($("#contain"));
},
error: function () {
alert("ajax failed!");
}
});
也就是,從App_all.xml這個文件中找到id為“id-1”的這一項,并將繼續(xù)在其子節(jié)點中找到auther標簽,并將其內(nèi)容加入到id 為contain
的div中,xml中我要查找的內(nèi)容為<auther>cocept</auther>,我就是想把cocept這個字段取出來,放入容器中
結(jié)果這段代碼在firefox中生效,在opera中也生效,但是在Safari和chrome中卻沒有效果,于是我上jQuery的官方論壇,和StackFlowover
論壇提問,后者有人回復我:
I assure you I was using $.ajax with Chrome just five hours ago at the office, and had no such problem.
I also imagine they use it here on SO and I have no problems here. I have no problems on jQueryUI in Chrome. I think it is your code.
大意就是斬釘截鐵的說他用起來沒有問題,是我自己的問題,我也納悶了,后面也有人給我的建議更復雜:
You should use chrome's or safari's built-in developer tools (ctrl+shift+i) to track JS errors and track actual AJAX requests.
Is your code wrapped in document.ready? Is there any erros in javascript console? Also try to output something after success callback line.
Another cause for this could be incorrect mime-type for your XML file returned by server. It should be [Content-type: text/xml].
You can check that in chrome's or safari's built-in developer tools - just look for headers tab when xml resource is selected.
If it 's actual problem, you may need to tweak web-server configuration (main config or .htaccess for apache) to return correct mime-type
畢竟是自學jquery ajax框架剛起步……就遇到這么棘手的問題,的確麻煩……
但是,由于我一個不經(jīng)意的發(fā)現(xiàn),問題迎刃而解……
我用firebug查看成功后的頁面元素狀態(tài),發(fā)現(xiàn):
<div id="contain">
<auther>cocept</auther>
</div>
我恍然大悟,原來這種直接用pretendTo插入的方法會連tagname也插入進去,難怪chrome和Safari不能識別(從另一個方面來說Firefox原來強大這么多……) 于是修改后的代碼如下:
success: function (xml) {
$("#contain").html($(xml).find("app[id='id-1']").find("auther").text());
}
先取出所需元素的text()的值,再以修改html的方法html()插入容器中,大功告成啦!測試均通過
復制代碼 代碼如下:
$.ajax({
type: "get",
url: "Database/App_all.xml",
dataType: "xml",
timeout: 2000,
beforeSend: function () {},
success: function (xml) {
$(xml).find("app[id='id-1']").find("auther").appendTo($("#contain"));
},
error: function () {
alert("ajax failed!");
}
});
也就是,從App_all.xml這個文件中找到id為“id-1”的這一項,并將繼續(xù)在其子節(jié)點中找到auther標簽,并將其內(nèi)容加入到id 為contain
的div中,xml中我要查找的內(nèi)容為<auther>cocept</auther>,我就是想把cocept這個字段取出來,放入容器中
結(jié)果這段代碼在firefox中生效,在opera中也生效,但是在Safari和chrome中卻沒有效果,于是我上jQuery的官方論壇,和StackFlowover
論壇提問,后者有人回復我:
I assure you I was using $.ajax with Chrome just five hours ago at the office, and had no such problem.
I also imagine they use it here on SO and I have no problems here. I have no problems on jQueryUI in Chrome. I think it is your code.
大意就是斬釘截鐵的說他用起來沒有問題,是我自己的問題,我也納悶了,后面也有人給我的建議更復雜:
You should use chrome's or safari's built-in developer tools (ctrl+shift+i) to track JS errors and track actual AJAX requests.
Is your code wrapped in document.ready? Is there any erros in javascript console? Also try to output something after success callback line.
Another cause for this could be incorrect mime-type for your XML file returned by server. It should be [Content-type: text/xml].
You can check that in chrome's or safari's built-in developer tools - just look for headers tab when xml resource is selected.
If it 's actual problem, you may need to tweak web-server configuration (main config or .htaccess for apache) to return correct mime-type
畢竟是自學jquery ajax框架剛起步……就遇到這么棘手的問題,的確麻煩……
但是,由于我一個不經(jīng)意的發(fā)現(xiàn),問題迎刃而解……
我用firebug查看成功后的頁面元素狀態(tài),發(fā)現(xiàn):
復制代碼 代碼如下:
<div id="contain">
<auther>cocept</auther>
</div>
我恍然大悟,原來這種直接用pretendTo插入的方法會連tagname也插入進去,難怪chrome和Safari不能識別(從另一個方面來說Firefox原來強大這么多……) 于是修改后的代碼如下:
復制代碼 代碼如下:
success: function (xml) {
$("#contain").html($(xml).find("app[id='id-1']").find("auther").text());
}
先取出所需元素的text()的值,再以修改html的方法html()插入容器中,大功告成啦!測試均通過
相關文章
jquery.form.js框架實現(xiàn)文件上傳功能案例解析(springmvc)
這篇文章主要為大家詳細介紹了jquery.form.js/springmvc文件上傳功能的實現(xiàn)步驟,使用的技術有jquery.form.js框架,以及springmvc框架,具有實用價值,感興趣的小伙伴們可以參考一下2016-05-05Jquery循環(huán)截取字符串的方法(多出的字符串處理成"...")
下面小編就為大家?guī)硪黄狫query循環(huán)截取字符串的方法(多出的字符串處理成"...")。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Jquery ajax執(zhí)行順序 返回自定義錯誤信息(實例講解)
由于Jquery中的Ajax的async默認是true(異步請求),如果想一個Ajax執(zhí)行完后再執(zhí)行另一個Ajax, 需要把async=false就可以了2013-11-11