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>这样就可以了,这是我以前做的一个,数据自己随便处理。 怎样把一个链表插入到数据库? java中继承TimerTask类后run()方法中返回值问题 ? jsp操作MS SQL 2000查询 本人想找份兼职工作!各位大侠有没有信息? 请教大家一个不应该问的问题? 小妹请各位SCJDgg帮个忙 我想要一个JSP+BEAN+MYSQL的分页程序! 问题可能有点怪 java des cbc加密 后.net去解密 IE10 Session 问题,新窗口打开多个Java Console 好奇怪的问题 求教如何判断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>这样就可以了,这是我以前做的一个,数据自己随便处理。