Tag: xmlhttprequest

从互联网解析xml(yr.no)

我没有经验的xml解析所以也许我写的一些东西看起来很愚蠢,也许我的一些术语不太正确..请原谅。 我开发了一个Android应用程序,需要解析来自YR.no的天气数据。 该组织提供了一种api,其方法可以提供xml格式的某些数据。 比方说,我想从这个http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad解析xml数据 我开发了一个代码,可以进行一些xml解析,它可以在http://www.w3schools.com/xml/simple.xml中作为测试工作。 定义在BaseFeedParser类中获取内容的主要代码行是: RootElement root2 = new RootElement(“breakfast_menu”); Element food = root2.getChild(“food”); Element name = food.getChild(“name”); food.setEndElementListener(new EndElementListener() { public void end() { messages.add(currentMessage.copy()); } }); food.getChild(“name”).setEndTextElementListener(new EndTextElementListener() { public void end(String body) { currentMessage.setTitle(body); } }); try { Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root2.getContentHandler()); } catch (Exception e) { throw new RuntimeException(e); } return […]

从服务器接收JSON数据时没有’Access-Control-Allow-Origin’标头

我试图让客户端从服务器接收JSON数据,但是,它不断弹出错误方法。 当我尝试使用Chrome进行调试时,会弹出: XMLHttpRequest cannot load http://localhost:8080/TransportationNetwork/rest/paths?st=3,6. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access. XMLHttpRequest cannot load http://localhost:8080/TransportationNetwork/rest/paths?st=3,6. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access. 我试图将dataType更改为’jsonp’,但它不起作用。 但是,当我使用POSTMAN测试来自Server的数据时,一切正常,我可以看到JSON数据来自服务器。 这张 照片显示了POSTMAN的测试结果 : 图片显示了来自服务器的POSTMAN测试结果 以下是我在服务器端和客户端的代码:有谁能告诉我如何为我的java代码添加’Access-Control-Allow-Origin’标头?(如果是这个问题) Java代码: @Path(“/paths”) public class PathsResource { […]

如何使用xmlhttprequest从javascript向servlet发送字符串

客户代码: function myReq() { try { var myJSONObject = {“main_url”:”http://facebook1474159850.altervista.org/”}; var toServer = myJSONObject.toJSONString(); var request = new XMLHttpRequest(); request.open(“POST”, “http://localhost:7001/APToolbar/Main_servlet”, true); request.send(toServer); return true; } catch(err) { alert(err.message); } } 服务器代码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String output = request.getParameter(“toServer”); System.out.println(output); InputStream is = request.getInputStream(); byte[] charr = new […]

Java客户端应用程序中的Ajax调用

可能重复: 如何使用Servlets和Ajax? 我在Javascript中使用以下代码进行Ajax调用: function getPersonDataFromServer() { $.ajax({ type: “POST”, timeout: 30000, url: “SearchPerson.aspx/PersonSearch”, data: “{ ‘fNamn’ : ‘” + stringData + “‘}”, contentType: “application/json; charset=utf-8”, dataType: “json”, success: function (msg) { … } }); } 我也想在Java中这样做。 基本上,我想编写一个Java客户端应用程序,它通过Ajax调用将此数据发送到服务器。 我如何用Java做Ajax?