简体   繁体   中英

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. The Second is that when I try to fetch data in JSON-format, not XML, I will always get a xml. I am really newbie in js, Jquery and things like that, so I just started to study it.

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:

40MarkFree1Lake20030SamWords2Grass105

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? Could somebody implement basic case to my js-function and explain why and what. My function is returning just [object Object],[object Object]?

  2. How to make data to be in JSON-format, not 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()?

  5. I am using JPA and JAAS-authentication. How to avoid cache-problem in the case that I am using web services and client can be in different server? Sometimes the data is old because Eclipselink is reading it from cache, not from the 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. 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

to get the JSON into JavaSCript, you can call

res = JSON.parse(responseJson);

res will become a JS Object where you can do anything easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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