日期:2014-05-17  浏览次数:22009 次

ajax调用webservice失败
<script type="text/javascript">
$(function(){
$("#getWeather").click(function(){
var city = $("#cityName").val();
//alert(city);
var soap = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getWeather xmlns="http://WebXml.com.cn/"><theCityCode>'+ city +'</theCityCode><theUserID>123</theUserID></getWeather></soap:Body></soap:Envelope>';
$.ajax({
url:'http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather',
type:'POST',
dataType: 'xml',
data:soap,
success:function(data){
                 alert('success');
             },
error:function() {
alert('error');
}
});

});
});
</script>


结果是调了error
控制台信息是:XMLHttpRequest cannot load http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 


求大神指教!

------解决方案--------------------
跨域访问 ajax跨域访问
------解决方案--------------------
你这是跨域ajax吧!?

跨域只能使用JSONP来实现,或者通过服务器端获取
------解决方案--------------------
楼主可以将webservice取数据放到后台去处理,然后数据返回页面就可以了。后台调用不涉及跨域的问题。
------解决方案--------------------
这是跨域ajax吧?
------解决方案--------------------
跨域了。不可以,你可以在后台调用webservice ,然后前台ajax' 获取就行了。
------解决方案--------------------
jsonp估计不好搞,callback这边控制不了。还是写个servlet吧。severlet调用webservice,然后前台调用这个servelet就行了。
 <script type="text/javascript">
            $.ajax({
                url:'http://localhost:8080/test/severlet/weather',
                type:'GET',
                dataType: 'json',
                success:function(data){
                    alert(data.h