首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

ajax调用webservice失败解决方案

2013-12-13 
ajax调用webservice失败script typetext/javascript$(function(){$(#getWeather).click(function()

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.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 ServletException, IOException {
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,


ProtocolException {
String s = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather";
URL url = new URL(s);
HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
http.setUseCaches(false);
http.setRequestProperty("Content-Type", "text/xml");
http.connect();

DataOutputStream out = new DataOutputStream(http.getOutputStream());
String city = "025";
String content = "<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></theUserID></getWeather></soap:Body></soap:Envelope>";
out.writeBytes(content);

out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(http
.getInputStream()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
// System.out.println(line);
buffer.append(line);
}
reader.close();
http.disconnect();
System.out.println(new String(buffer.toString().getBytes("gbk"),
"utf-8"));
return buffer.toString();
}

}



web.xml


<servlet>
<servlet-name>weatherServlet</servlet-name>
<servlet-class>com.WeatherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>weatherServlet</servlet-name>
<url-pattern>/severlet/weather</url-pattern>
</servlet-mapping>




这样就可以了,这是我以前做的一个,数据自己随便处理。

热点排行