Demo project for Spring Boot 【1】spring-boot-starter【2】Consuming a RESTful Web Service with jQuery

This controller module is represented as a simple JavaScript function. It uses jQuery’s $.ajax() method to consume the REST service at http://rest-service.guides.spring.io/greeting. If successful, it will assign the JSON received to data, effectively making it a Greeting model object. The id and content are then appended to the greeting-id and greeting-content DOM elements respectively.
Note the use of the jQuery promise .then(). This directs jQuery to execute the anonymous function when the $.ajax() method completes, passing the data result from the completed AJAX request.

这个控制器模块被表示为一个简单的JavaScript函数。它使用jQuery的 . a j a x ( ) 方 法 在 h t t p : / / r e s t − s e r v i c e . g u i d e s . s p r i n g . i o / g r e e t i n g 。 如 果 成 功 , 它 将 把 接 收 到 的 J S O N 分 配 给 数 据 , 使 其 成 为 一 个 问 候 模 型 对 象 。 然 后 , i d 和 内 容 分 别 附 加 到 g r e e t i n g − i d 和 g r e e t i n g − c o n t e n t − D O M 元 素 。 注 意 j Q u e r y p r o m i s e . t h e n ( ) 的 用 法 。 这 将 指 导 j Q u e r y 在 .ajax()方法在http://rest-service.guides.spring.io/greeting。如果成功,它将把接收到的JSON分配给数据,使其成为一个问候模型对象。然后,id和内容分别附加到greeting-id和greeting-content-DOM元素。 注意jQuery promise.then()的用法。这将指导jQuery在 .ajaxhttp://restservice.guides.spring.io/greetingJSON使idgreetingidgreetingcontentDOMjQuerypromise.thenjQuery.ajax()方法完成时执行匿名函数,传递来自已完成ajax请求的数据结果。

Demo project for Spring Boot 【1】spring-boot-starter【2】Consuming a RESTful Web Service with jQuery_第1张图片

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoJQueryControllerApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoJQueryControllerApplication.class, args);
	}

}

<!DOCTYPE html>
<html>
    <head>
        <title>Hello jQuery</title>
        <script src="/jquery/jquery.min.js"></script>
        <script src="/js/hello.js"></script>
        <script src="/js/ajax.js"></script>
        <script type="text/javascript">
         $(document).ready(function() {
    	  $("button").click(function(){
    		  $('.greeting-id').append("!");
    	      $('.greeting-content').append("!");
    		  });
          });
        </script>
    </head>
    
    <body>
        <div>
            <p class="greeting-id">The ID is </p>
            <p class="greeting-content">The content is </p>
            <p id="id">1</p>
            <p id="content">hello</p>
            <button>is</button>
        </div>
    </body>
</html>
$(document).ready(function() {
    $.ajax({
        url: "http://localhost:8080/"
    }).then(function(data) {
       $('.greeting-id').append(data.id);
       $('.greeting-content').append(data.content);
    });
});
$(document).ready(function() {
	  $('.greeting-id').append("1");
      $('.greeting-content').append("Hello world!");

});

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-08-04 20:30:15.295  INFO 15628 --- [           main] c.e.d.DemoJQueryControllerApplication    : Starting DemoJQueryControllerApplication on PC-202008012041 with PID 15628 (G:\eclipse-workspace-Jee\demo-jQueryController\target\classes started by Administrator in G:\eclipse-workspace-Jee\demo-jQueryController)
2020-08-04 20:30:15.298  INFO 15628 --- [           main] c.e.d.DemoJQueryControllerApplication    : No active profile set, falling back to default profiles: default
2020-08-04 20:30:16.411  INFO 15628 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-04 20:30:16.419  INFO 15628 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-04 20:30:16.419  INFO 15628 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-04 20:30:16.422  INFO 15628 --- [           main] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.24] using APR version [1.7.0].
2020-08-04 20:30:16.423  INFO 15628 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-08-04 20:30:16.423  INFO 15628 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2020-08-04 20:30:16.427  INFO 15628 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1g  21 Apr 2020]
2020-08-04 20:30:16.527  INFO 15628 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-04 20:30:16.528  INFO 15628 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1171 ms
2020-08-04 20:30:16.700  INFO 15628 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-04 20:30:16.760  INFO 15628 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2020-08-04 20:30:16.882  INFO 15628 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-04 20:30:16.890  INFO 15628 --- [           main] c.e.d.DemoJQueryControllerApplication    : Started DemoJQueryControllerApplication in 2.009 seconds (JVM running for 3.773)
2020-08-04 20:30:20.949  INFO 15628 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-04 20:30:20.950  INFO 15628 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-08-04 20:30:20.957  INFO 15628 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms

Demo project for Spring Boot 【1】spring-boot-starter【2】Consuming a RESTful Web Service with jQuery_第2张图片Demo project for Spring Boot 【1】spring-boot-starter【2】Consuming a RESTful Web Service with jQuery_第3张图片

你可能感兴趣的:(springboot)