ajax调用webservice失败 跨域访问 ajax跨域访问 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这是跨域ajax吧!?跨域只能使用JSONP来实现,或者通过服务器端获取 楼主可以将webservice取数据放到后台去处理,然后数据返回页面就可以了。后台调用不涉及跨域的问题。 跨域了。不可以,你可以在后台调用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>这样就可以了,这是我以前做的一个,数据自己随便处理。 新手求关于jsp随机数问题 简单的DOM4j 讨论帖 File "/WEB-INF/struts-tiles.tld" not found unitTest, 这种情况要不要做? 小问题!大麻烦! 我在网页里面放了一个avi文件,怎么才能防止别人下载? 怎样取得web-inf下的路径 我在关闭浏览器窗口之前更改数据的字段,如何处理呢 如何作到一个连接中产生的多个记录集都正常使用 java操作Oracle数据库中的CLOB数据类型中我遇到的问题?希望大家的帮助 好奇怪的问题 求教如何判断url是否可用
$.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>这样就可以了,这是我以前做的一个,数据自己随便处理。