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>
<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>