how to connect javascript to webservice?

Asked

Viewed 99 times

0

After days and days of research, I come to cry out to all of you to save my semester.

What I want is very simple: connect my javascript front-end with my java web service REST.

Just as a test, I created a GET method that returns all elements of my database (in xml, because JSON is not working) :

@GET
@Override
@Path("teste")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Aluno> findAll() {
    return super.findAll();
}

well. The problem is that I have no idea what to do in my javascript for: 1. communicate with this website and 2: capture the values returned from this xml

my javascript (that is not funncionando, this code got here on the forum):

 $(document).ready(function(){
$.ajax({
    url: "http://192.168.1.100:8080/SemanaEngenharia/webresources/service.aluno/teste",
    sucess: function(data) {
        alert(data);
        document.getElementByClassName(tituloFormulario).value = data;
    }
});
});
  • At first there are two errors: the correct one is getElementsByClassName ("Elements" plural) and an index is needed, because the getElementsByClassName returns a nodelist, soon would be document.getElementByClassName(tituloFormulario)[0].value = data; to change the value of the first element of the class. The index [0] is the first element.

  • And the variable tituloFormulario must be a class name.

  • sam, thanks for the reply, I made the changes you suggested (Document.getElementsByClassName(titleFormulario)[0].value = date;) and yes, titleFormulario is the name of a class of a randomized field that I used only to see the return of the web service. But when updating the page, it has no effect, I have no idea what could be wrong :/

  • Missing a "c" in "Success".

  • also fixed haha. The funny thing is that this code prevents my javascript from working, when I remove it, it works normally again

  • You uploaded the jQuery library?

  • Yes, everything is working normally. My real problem is that I don’t know how to communicate javascript with the webservice, so I can’t tell what’s wrong.

Show 3 more comments

1 answer

0

I suggest you study how to return a JSON using Jaava and then try to connect, take the date from Alert and place a console.log()

console.log(data)

Another suggestion is to look if there is an error in Chrome devtools. Possibly it may be CORS problem, you have to tell your server, that this address can access the necessary resources. Let us know or put your project on github so we can understand the context.

  • Hello Iago! Thanks for the help, really it was the CORS that was in the way. Now I’m trying to save a form in the database through ajax, but I don’t understand how it works, Voce knows?

  • currently my code is: (host in image format not to get confused) https://ibb.co/hKUhkL where url loads the way to save in my web service, this method is like this: https://ibb.co/dEjF5L Now I do not know how to send the form through ajax as JSON, in order for the webservice to receive and save in the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.