Javascript 如何在JS中从http get方法写入控制台字符串文本

Javascript 如何在JS中从http get方法写入控制台字符串文本,javascript,ajax,spring-boot,get,Javascript,Ajax,Spring Boot,Get,我的Spring Boot应用程序上有Rest API: @CrossOrigin @GetMapping() public String getText(@RequestHeader(value = "token") String token) { if (token == "1") { return "Hello world"; } r

我的Spring Boot应用程序上有Rest API:

  @CrossOrigin
        @GetMapping()
        public String getText(@RequestHeader(value = "token") String token)
        {
           if (token == "1") {      
             return "Hello world";
           }

           return "511 ";
        }
我的UI上有JS Ajax get请求:

    <script type="text/javascript">
      function login() {    
    const url = 'http://localhost:8085/api';
    const otherPram= {
    headers: {
     'Content-Type':  'application/json',
      'token': 'test'
    },
    method:"GET"
    }

    fetch(url,otherPram).then(res=>{console.log(res)})
   }
    </script>

函数login(){
常量url=http://localhost:8085/api';
const otherPram={
标题:{
“内容类型”:“应用程序/json”,
“令牌”:“测试”
},
方法:“获取”
}
fetch(url,otherPram).then(res=>{console.log(res)})
}

我想在控制台中打印我的返回类型“Hello world”。

您的端点返回一个普通字符串,因此您必须提取响应的文本表示形式

fetch(url, otherPram)
.then(res => res.text())
.then(text => console.log(text));

获取API

很高兴它有帮助:)