简体   繁体   English

当我请求停止服务(春季启动)时,jQuery无法正常工作

[英]Jquery does not work when I request to rest service (spring boot)

I'm working on a spring boot rest api and I want to take information from rest api to a html page. 我正在开发spring boot rest api,我想将信息从rest api获取到html页面。 I tried this using jquery but I could not take data. 我尝试使用jquery进行此操作,但无法获取数据。

Here is my Rest Controller 这是我的休息控制器

package tr.kasim.Controller;

import java.sql.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tr.kasim.Service.PersonelService;
import tr.kasim.Service.PtsGirisCikisService;
import tr.kasim.Model.Personel;
import tr.kasim.Model.PtsGiriscikis;

@CrossOrigin("*")
@RestController
public class STRestController {

@Autowired
public PersonelService personelService;

@Autowired
public PtsGirisCikisService ptsGirisCikisService;

@GetMapping("/api/personels")
public ResponseEntity<List<Personel>> getPersonels(){
    List<Personel> personels = personelService.findAll();
    return ResponseEntity.ok().body(personels);
}

@GetMapping("/api/giriscikis")
public ResponseEntity<List<PtsGiriscikis>> getGirisCikis(){
    List<PtsGiriscikis> giriscikis = ptsGirisCikisService.findAll();
    return ResponseEntity.ok().body(giriscikis);
}

//@GetMapping("/api/personel")
@RequestMapping(method=RequestMethod.GET, value="/api/personel", produces="application/json" )
public ResponseEntity<List<PtsGiriscikis>> getPersonelByKartNoAndİseGiris(@RequestParam("kartno") String KartNo , @RequestParam("tarih") Date IseGirisTarihi){
    String errorMessage="Aranilan Personel Bulunamadi!";

    Personel personel = personelService.findPersonelByKartnoAndIsegiristarihi(KartNo , IseGirisTarihi);

    if(!personel.equals(null)) {
        List<PtsGiriscikis> girisCikis = ptsGirisCikisService.findByPersonelid(personel.getId());
        return ResponseEntity.ok().body(girisCikis);
    }

        return null;
}

}

Here is my Controller 这是我的控制器

package tr.kasim.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class STController {

@GetMapping("/")
public String goHome() {
    return "templates/index";
}
}

Here is index.html page 这是index.html页面

<!DOCTYPE html>
<html>
<head>
    <title>Hello jQuery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        function findPersonels() {
            $.ajax({
                type:"GET",
                url: "/api/personels",
                contentType: "application/json"
            }).then(function(data) {
                $('.giriscikis-id').append(data.id);
                $('.giriscikis-ad').append(data.ad);
            });
        }
    </script>
</head>

<body>
    <div>

        <p class="giriscikis-id">The ID is </p>
        <p class="giriscikis-ad">The name is </p>
        <input type="button" id="doldur" name="doldur" value="field doldur" onclick="findPersonels();">
    </div>
</body>

I tried many jquery + spring application examples but I still got nothing. 我尝试了许多jquery + spring应用程序示例,但仍然一无所获。 When I tried to reach index.html I just saw my html writings, rest service information are not showing. 当我尝试访问index.html时,只看到了我的html著作,其余服务信息却未显示。 Please correct me, thank you. 请纠正我,谢谢。

With the limited information: 信息有限:

  1. I guess your mapping from path to template is wrong. 我猜您从路径到模板的映射是错误的。 Shouldn't it be like this? 难道不是这样吗?

    @GetMapping("/") public String goHome() { return "templates/index"; @GetMapping(“ /”)public String goHome(){返回“模板/索引”; } }

  2. An Uncaught ReferenceError indicates that your method is not found. Uncaught ReferenceError表示找不到您的方法。 Please verify the spelling (looks fine in your example, please check again in your code). 请验证拼写(在您的示例中看起来不错,请在代码中再次检查)。 In your example the braces of findPersonels are totally messed up. 在您的示例中,findPersonels的括号完全被弄乱了。 There is too many ) . 有太多) If that is corrected check in your browser console that there is no http404 when loading the page (so that js/personels.js is found). 如果已解决问题,请在浏览器控制台中检查是否在加载页面时没有http404 (以便找到js/personels.js http404 )。

  3. Your endpoint /api/personels defines a list as return type but your script uses the properties data.ad and data.id which do not look like they expect a list. 端点/api/personels将列表定义为返回类型,但是脚本使用的属性data.addata.id看起来并不像他们期望的列表。 The returned json probably looks something like this { items: [ { 'ad': 'first personel' 'id': '1' }, { 'ad': 'second personel' 'id': '2' } ] } The structure depends on your JSON marshaller. 返回的json可能看起来像这样{ items: [ { 'ad': 'first personel' 'id': '1' }, { 'ad': 'second personel' 'id': '2' } ] }结构取决于您的JSON编组器。 So a correct syntax for accessing your object in JS would be somehting like data.items[0].id . 因此,在JS中访问对象的正确语法类似于data.items[0].id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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