关于jquery获取服务器端xml数据

时间:2022-12-02 08:07:37

其实这个很简单,但是有时候简单的东西会让你犯一些低级错误。

今天写了个spring mvc 的服务器端接口,主要是用来共享一些数据库数据。

请求字段:http://localhost:8080/XXXXX/data/listrealdata?m_pname=%25%25&m_pp=%25%25&m_ggxh=%25%25&m_xsht=%25%25&m_pid=%25%25

返回数据:

his XML file does not appear to have any style information associated with it. The document tree is shown below.
<kFdatalist>
<catalog>1</catalog>
<kfdatas>
<kfdata>
<id>1397</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-14</kf_pid>
<kf_pname>触摸屏</kf_pname>
<kf_ggxh>6AV2124-0GC01-0AX0</kf_ggxh>
<kf_pp>西门子</kf_pp>
<kf_dw></kf_dw>
<kf_num>4</kf_num>
<kf_pricetax>3376.0700</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>4</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1398</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-15</kf_pid>
<kf_pname>模块</kf_pname>
<kf_ggxh>6ES7223-1PH22-OXA8</kf_ggxh>
<kf_pp>西门子</kf_pp>
<kf_dw></kf_dw>
<kf_num>40</kf_num>
<kf_pricetax>423.9300</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>40</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1399</id>
<kf_rkry/>
<kf_rkdate>1900-01-01T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-16</kf_pid>
<kf_pname>线鼻子(红绿蓝)</kf_pname>
<kf_ggxh>BBC</kf_ggxh>
<kf_pp>B</kf_pp>
<kf_dw>B</kf_dw>
<kf_num>10</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght>TEST-006</kf_cght>
<kf_cglx>补充库存</kf_cglx>
<kf_xsht>补充库存</kf_xsht>
<kf_xsname>补充库存</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>10</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>GFS-RK-41</kf_rkdh>
</kfdata>
<kfdata>
<id>1400</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-17</kf_pid>
<kf_pname>防爆箱</kf_pname>
<kf_ggxh>560*430*197</kf_ggxh>
<kf_pp>合隆</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>3589.7400</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1401</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-18</kf_pid>
<kf_pname>模块</kf_pname>
<kf_ggxh>6ES7223-1BH22-OXA8</kf_ggxh>
<kf_pp>西门子</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>408.5500</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1402</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-26</kf_pid>
<kf_pname>模块</kf_pname>
<kf_ggxh>6ES7235-0KD22-OXA8</kf_ggxh>
<kf_pp>西门子</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>704.2700</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1403</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-27</kf_pid>
<kf_pname>温度变送器</kf_pname>
<kf_ggxh>QAH11.1</kf_ggxh>
<kf_pp>西门子</kf_pp>
<kf_dw></kf_dw>
<kf_num>1</kf_num>
<kf_pricetax>85.4700</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>1</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1404</id>
<kf_rkry>admin</kf_rkry>
<kf_rkdate>2016-12-27T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-28</kf_pid>
<kf_pname>模块</kf_pname>
<kf_ggxh>MO</kf_ggxh>
<kf_pp>斯菲尔</kf_pp>
<kf_dw></kf_dw>
<kf_num>1</kf_num>
<kf_pricetax>299.1500</kf_pricetax>
<kf_cght>TEST-001</kf_cght>
<kf_cglx/>
<kf_xsht>GFS-1207020</kf_xsht>
<kf_xsname>管坯排锯</kf_xsname>
<kf_adress1/>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>1</kf_num_invalid>
<kf_rkdh>GFS-RK-52</kf_rkdh>
</kfdata>
<kfdata>
<id>1405</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-34</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 0.5黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>123</kf_rkdh>
</kfdata>
<kfdata>
<id>1406</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-35</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 1.0黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>1234</kf_rkdh>
</kfdata>
<kfdata>
<id>1407</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-36</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 1.5黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh>15</kf_rkdh>
</kfdata>
<kfdata>
<id>1408</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-37</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 10黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1409</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-38</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 16黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1410</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-39</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 25黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1411</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-40</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>E304362</kf_ggxh>
<kf_pp>进口</kf_pp>
<kf_dw></kf_dw>
<kf_num>2000</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1412</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-41</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>2芯视频线</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1413</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-42</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>12*1 蓝</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1414</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-43</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>YZ 6*1.0</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1415</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-44</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>RVV 10*1</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<kfdata>
<id>1416</id>
<kf_rkry/>
<kf_rkdate>2017-01-04T00:00:00+08:00</kf_rkdate>
<kf_pid>GFS-KF-45</kf_pid>
<kf_pname>电缆</kf_pname>
<kf_ggxh>BVR 4.0黑</kf_ggxh>
<kf_pp>国产</kf_pp>
<kf_dw></kf_dw>
<kf_num>0</kf_num>
<kf_pricetax>0.0000</kf_pricetax>
<kf_cght/>
<kf_cglx/>
<kf_xsht/>
<kf_xsname/>
<kf_adress1>A-1</kf_adress1>
<kf_adress2/>
<kf_num_valid>0</kf_num_valid>
<kf_num_invalid>0</kf_num_invalid>
<kf_rkdh/>
</kfdata>
<offset>1</offset>
<size>20</size>
<total>20</total>
</kfdatas>
<newsCount>20</newsCount>
<pageSize>0</pageSize>
</kFdatalist>

数据有点长,仅供参考了。

一切很顺利,用Fiddler测试一切顺利,准备做个html页做个简单的测试页。

但是遇到了点问题,

HTML源码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>加载XML</title>

<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(
function(){
$(
"#send").click(function(){
$.ajax({
type :
'GET',
dataType :
'xml',
async:
false,
data: {
m_pname : $(
"#m_pname").val() ,
m_pp : $(
"#m_pp").val() ,
m_ggxh : $(
"#m_ggxh").val() ,
m_xsht : $(
"#m_xsht").val() ,
m_pid : $(
"#m_pid").val()
},
url:
"http://localhost:8080/spring_ht/data/listrealdata",
timeout:
2000,
error:
function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
success:
function(xml)
{
$(xml).find(
"kfdata").each(function(i)
{
var kf_pname = $(this).children("kf_pname").text();
var kf_ggxh = $(this).children("kf_ggxh").text();
var kf_pp = $(this).children("kf_pp").text();
var kf_num = $(this).children("kf_num").text();
var kf_rkdh = $(this).children("kf_rkdh").text();

var html = '';
html
+= '<div class="kf_pname"><h6>'+kf_pname+
'<div class="kf_ggxh"><h6>'+kf_ggxh+
'<div class="kf_pp"><h6>'+kf_pp+
'<div class="kf_num"><h6>'+kf_num+
'<div class="kf_rkdh"><h6>'+kf_rkdh
$(
'#resText').html(html);
});
}
});
})
})
</script>
</head>
<body>
<form id="form1" action="#">
<p>分页测试:</p>
m_pname:
<input id="m_pname" name="m_pname" type="text" value="%%"/>
m_pp:
<input id="m_pp" name="m_pp" type="text" value="%%"/>
m_ggxh:
<input id="m_ggxh" name="m_ggxh" type="text" value="%%"/>
m_xsht:
<input id="m_xsht" name="m_xsht" type="text" value="%%"/>
m_pid:
<input id="m_pid" name="m_pid" type="text" value="%%"/><br/>
<p><input type="Button" id="send" value="提交"/></p>
</form>
<div class='comment'>返回数据:</div>
<div id="resText" ></div>

</body>
</html>

一个很简单的东西,以上的代码是解决了问题的:

问题现象是,我提交后,网页提示error。

关于jquery获取服务器端xml数据

解决办法是:

<p><input type="button" id="send" value="提交"/></p>
改成
<p><input type="Button" id="send" value="提交"/></p>

希望;雷同问题的小朋友少走弯路...