通过ajax将表单数据传递给spring控制器;一律收到404错误

时间:2019-08-26 14:46:27

标签: javascript ajax spring

我正在尝试将数据从我的.jsp传递到spring控制器。但是我的ajax调用总是会出错。

我尝试过警报和console.log是否捕获了数据,这很好。我猜我的ajax调用/控制器出错。

$.ajax({
    type: "POST",
    url:"/save",
    async: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
        "registrationNumber" : regNo,
        "GPAYearI" : gpaI,
        "GPAYearII" : gpaII
    }),

    success: function(){
        alert('Saved');
    }, error: function (response) {
        console.log("Error occurred: " + response.responseText);
    }
});

这是我的控制器,

@RequestMapping("/")
    public class StudentController {
    @Autowired
    StudentRepository studentRepository;

    @PostMapping("save")
    public ResponseEntity<?> createCase(@RequestBody StudentDTO studentDTO){
        Student studentCase = new Student();
        studentCase.setRegistrationNumber(studentDTO.getRegistrationNumber());
        studentCase.setGPAYearI(studentDTO.getGPAYearI());
        studentCase.setGPAYearII(studentDTO.getGPAYearII());

        studentRepository.save(studentCase);
        return new ResponseEntity<String>(studentCase.getRegistrationNumber(), HttpStatus.CREATED);
    }
}

我在StudentDTO中也有相同的实体。

Error occurred: {"timestamp":"2019-08-26T14:08:29.460+0000","status":404,"error":"Not Found","message":"No message available","path":"/save"} student.js:51:21

以上是我收到的消息。

我想知道我在哪里做错了。

0 个答案:

没有答案
相关问题