【发布时间】:2013-01-19 02:11:18
【问题描述】:
我想用 JavaScript 处理一个 http-request 的响应。你可以在这里找到一个简单的例子。
<!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 id="Head1">
<title>JavaScript Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function serviceCall() {
$.ajax({
type: "GET",
url: 'http://localhost:8181/geoserver/wfs?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326',
// url: 'http://demo.opengeo.org/geoserver/ows?Service=WFS&Request=GetFeature&Version=1.0.0&typename=topp:tasmania_water_bodies&SRS=EPSG:4326',
complete: function(xml, status){
alert(xml.responseText);
}
});
}
</script>
</head>
<body>
<center><button onclick="serviceCall()">Start...</button></center>
</body>
</html>
请求直接在浏览器中运行。通过 Ajax 和 JavaScript,响应为空。 Firebug 在第 1 行第 1 列报告 xml 解析错误。我尝试将请求发送到 localhost 和远程服务器,但响应始终为空。如有任何建议,我将不胜感激。
【问题讨论】:
-
您对响应内容的期望是什么?
-
为什么你处理的是完成而不是ajax的成功事件?
-
如果不仅仅是一些无效的 XML,它可能是一个跨域 ajax 问题
-
同意@ryadavilli——可能您的请求失败(可能是由于相同的来源策略)并且您的jqXHR 对象没有任何
responseText。但是,您看不到这一点,因为您没有success或error处理程序(并且无论成功或失败,complete处理程序都会触发)。
标签: javascript jquery ajax