$.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.he);
                },
                error : function(e, text){
                    alert(e.status);
                    alert(text);
                }
            });

</script>





public class WeatherServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletExceptionIOException {
doGet(req, resp);
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin", "*");

String json = Test.getWeatherInfo();
json = new String(json.getBytes("gbk"), "utf-8");
JSONObject obj = new JSONObject();
out.write(obj.put("he", json.toString()).toString());
out.flush();
out.close();
}
}


public class Test {

static String getWeatherInfo() throws MalformedURLException, IOException,
查看全文
  相关解决方案