简体   繁体   English

使用JavaScript从Restful Web服务中获取数据?

[英]Fetching data from Restful webservice with JavaScript?

I've got a couple of problems. 我有几个问题。 The first is that I don't know how to fetch data from webservice with javascript. 首先是我不知道如何使用javascript从网络服务获取数据。 The Second is that when I try to fetch data in JSON-format, not XML, I will always get a xml. 第二个是,当我尝试以JSON格式而不是XML来获取数据时,我总是会得到一个xml。 I am really newbie in js, Jquery and things like that, so I just started to study it. 我真的是JS,Jquery之类的新手,所以我才开始研究它。

http://localhost:8080/UserRest/webresources/users?format=json // Even If format is JSON the result is always XML. Browser is Chrome


<users>
  <user>
    <age>40</age>
    <firstName>Mark</firstName>
    <freeText>Free</freeText>
    <id>1</id>
    <lastName>Lake</lastName>
    <weight>200</weight>
  </user>
  <user>
    <age>30</age>
    <firstName>Sam</firstName>
    <freeText>Words</freeText>
    <id>2</id>
    <lastName>Grass</lastName>
    <weight>105</weight>
  </user>
</users>

And if I try 如果我尝试

http://localhost:8080/UserRest/webresources/users?format=XML

this in Safari, I got: 在Safari中,我得到了:

40MarkFree1Lake20030SamWords2Grass105 40马克免费1湖20030山姆字2草105

Webservice: 网络服务:

@GET 
@Override
@Produces({"application/xml", "application/json"})
public List<User> findAll() {
    return super.findAll();
}

Page: 页:

      function fetchUserFunction()
        {
            $.getJSON("http://localhost:8080/UserRest/webresources/users?format=json",

        function(data) {
            $.each(data, function(key, value) {
                    alert(key + "=" + value);
                    $('#maindiv').append('<li id="' + key.toString() + '">' + value.toString() + '</li>');
                });
        });

        }
    </script>
</head>
    <body>
        <h1>Users</h1>
        <a href="http://localhost:8080/UserRest/webresources/users"> Test restful service</a>
        <button onclick="fetchUserFunction()">Fetch Users</button>
        <div id="maindiv"></div>
    </body>
</html>

QUESTIONS: 问题:

  1. How to fetch data from a page with JavaScript using JSON or/and Jquery or just pure js? 如何使用JSON或/和Jquery或仅使用纯js从JavaScript提取页面数据? Could somebody implement basic case to my js-function and explain why and what. 有人可以对我的js函数实现基本情况并解释原因和原因。 My function is returning just [object Object],[object Object]? 我的函数只返回[object Object],[object Object]吗?

  2. How to make data to be in JSON-format, not XML? 如何使数据为JSON格式而不是XML?

  3. What is the best way to build a table from the data fetched? 从获取的数据构建表的最佳方法是什么?

  4. Can I call a specific metod in webservice class and how, like findAll()? 我可以在webservice类中调用特定的方法,如何调用,例如findAll()?

  5. I am using JPA and JAAS-authentication. 我正在使用JPA和JAAS身份验证。 How to avoid cache-problem in the case that I am using web services and client can be in different server? 在我使用Web服务并且客户端可以位于不同服务器的情况下,如何避免缓存问题? Sometimes the data is old because Eclipselink is reading it from cache, not from the DB? 有时数据是旧的,因为Eclipselink是从缓存而不是从DB读取数据的?

  6. How is your day? 你过得好吗?

  7. Is this suitable to ask many questions in one? 这适合一个人问很多问题吗?

Sami 佐美

@GET
@Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
    ...
}

The doGetAsXmlOrJson method will get invoked if either of the media types "application/xml" and "application/json" are acceptable. 如果可接受的媒体类型为“ application / xml”和“ application / json”,则将调用doGetAsXmlOrJson方法。 If both are equally acceptable then the former will be chosen because it occurs first. 如果两者均可接受,则将选择前者,因为它首先出现。

found this at http://jersey.java.net/nonav/documentation/snapshot/jaxrs-resources.html http://jersey.java.net/nonav/documentation/snapshot/jaxrs-resources.html找到了

to get the JSON into JavaSCript, you can call 要将JSON转换为JavaSCript,您可以调用

res = JSON.parse(responseJson);

res will become a JS Object where you can do anything easily. res将成为一个JS对象,您可以在其中轻松进行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM