Spring MVC没有通过ajax请求获得任何结果

时间:2018-09-16 08:34:28

标签: javascript ajax spring-mvc

我想从ajax请求中获取有关我的用户的数据。但是它永远不会记录任何内容,这意味着它永远不会到达ajax请求的成功部分。

这是我的控制人

@Controller
@RequestMapping(value = "/api/profile")
public class ProfilController {

@Autowired
public UserService userService;

@RequestMapping(value = "/show/{username}", method = RequestMethod.GET)
public ResponseEntity<UserDTO> showData(@PathVariable String username) {
    User u = userService.findByUsername(username);
    if(!userService.findAll().contains(u))
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    return new ResponseEntity<>(new UserDTO(u), HttpStatus.OK);
  }
}

这是我的Java脚本文件。

$(document).ready(function() {
console.log(localStorage.getItem('loggedIn'));
var usrnm = localStorage.getItem('loggedIn');

$.ajax({
  url: "http://localhost:8080/api/user/login/check/"+usrnm,
  type: "GET",
  headers: {"Authorization": localStorage.jwt},
  success: function(data) {
    console.log('success');
  }
})

$.ajax({
    url: "http://localhost:8080/api/profile/show/"+usrnm,
    type: "GET",
    success: function(data) {
        console.log('This part is not executed');
    }
  })
});

我是Spring的新手,实际上我是编程的新手,所以很抱歉,如果这个问题的格式不正确

2 个答案:

答案 0 :(得分:0)

您可能在showData函数中出错。您应该执行以下操作:     @PathVariable("username") String username

答案 1 :(得分:0)

尝试删除它。

@Controller
@RequestMapping("/api/profile") // fixed this!!! remove `value = `
public class ProfilController {
    @Autowired
    public UserService userService;

    @RequestMapping(value = "/show/{username}", method = RequestMethod.GET)
    public ResponseEntity<UserDTO> showData(@PathVariable String username) {
        User u = userService.findByUsername(username);
        if(!userService.findAll().contains(u))
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        return new ResponseEntity<>(new UserDTO(u), HttpStatus.OK);
      }
    }