json - How to consume the REST service with ajax(jQuery) in Spring Boot? -


i'm following official spring doc on topic https://spring.io/guides/gs/consuming-rest-jquery/#_create_the_application_page

maybe there's else omitted/missing, can't working me (intellij, springboot).

i have same greeting(from repo https://github.com/spring-guides/gs-rest-service/tree/master/complete/src/main/java/hello), greetingcontroller classes, latter supposed return json data. both controllers, tested apart, work well. "index.html" , "hello.js" copied tee.

but far understand, working 1 needs redirect client "index.html" page, purpose i've added controller.

finally, complete project structure:

src |--main    |---java        |---com.example.demo            |---controller                greetingcontroller.java                clientcontroller.java            |---model                greeting.java        demoapplication.java   |---resources       |---static.js              hello.js       |---templates              index.html       application.properties 

hello.js reads as:

$(document).ready(function() { $.ajax({     url: 'https://localhost:8080/greeting' }).then(function(data) {     $('.greeting-id').append(data.id);     $('.greeting-content').append(data.content); }); 

});

index.html:

<!doctype html>  <html lang="en">  <head>      <title>hello jquery</title>        <!--alt+enter download library in case it's not there-->      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>      <script src="../static/hello.js"></script>  </head>    <body>  <div>      <p class="greeting-id">the id </p>      <p class="greeting-content">the content </p>  </div>  </body>  </html>

as can see, hello.js has following function:

url: 'https://localhost:8080/greeting' 

as far see, supposed access address, , response received provided greetingcontroller, , parsed json object. get:

the id is
content is

what's wrong? difference tutorial have own usual spring controller instead of groovy one. else need glued successfully?

in comments clarified javascript file wasn't found. fix had to

  1. change script location <script src="hello.js"></script> since spring boot looks static files under src/main/resources/static default
  2. also url had http instead of https.

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -