Spring MVC Not getting back any results with ajax request

3078 views javascript
5

I want to get data about my user from ajax request. But it never logs anything, which means it never reaches to success part of ajax request.

This is my controller

@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);
  }
}

And this is my java script file.

$(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');
    }
  })
});

I am new to Spring, actually I am new to programming so sorry if this question is not well formatted

answered question

request & response are nothing? show me network status.

1 Answer

1

You may have an error in the showData function. You should do the following: @PathVariable("username") String username

posted this

Have an answer?

JD

Please login first before posting an answer.