我从JavaScript调用http://wsf.cdyne.com/WeatherWS/Weather.asmx网络服务。我将数据邮政编码传递为10007(硬编码)。想要获得一些输出数据。但是当我运行代码成功消息时会发出警报,然后警告弹出说没有。似乎没有返回数据。任何人都可以编辑代码以返回正确的输出吗?
<html>
<head>
<title>Calling Web Service from jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnCallWebService").click(function (event) {
$.ajax({
type: "POST",
url: "http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "10007",
success: processSuccess,
error: processError
});
});
});
function processSuccess(data, status, req) { alert('success');
if (status == "success")
$("#response").text($(req.responseXML).find("Result").text());
alert(req.responseXML);
}
function processError(data, status, req) {
alert('err'+data.state);
//alert(req.responseText + " " + status);
}
</script>
</head>
<body>
<h3>
Calling Web Services with jQuery/AJAX
</h3>
<input id="btnCallWebService" value="Call web service" type="button" />
<div id="response" ></div>
</body>
</html>
答案 0 :(得分:0)
试试这个
$.ajax({
url: "URL",
dataType: "json",
data: {term: request.term},
success: function(data) {
alert(data);
var dData = JSON.parse(data);
alert(dData.Name); //get your stuff
}
});
答案 1 :(得分:0)
你启用了CORS吗? 当我在firebug中测试时,我得到的主题如下所示
阻止跨源请求:同源策略禁止在http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。
这些链接可能对您有所帮助