1

Here I try to make a simple post request to my controller with an url as post body. After ajax call completes, I don't receive the response from the controller. Why ?

This is my controller:

@RestController
public class Controller {
    @RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = {"application/json"})
    public ResponseEntity<String> authenticate(@RequestBody String url) {
        System.out.println(url);

        return new ResponseEntity<>("TOKEN", HttpStatus.OK);
    }
}

And the ajax call:

var url = 'https://slack.com/api/oauth.access' +
        '&client_id=' + client_id + '&client_secret=' + client_secret + '&code=' + code + '&redirect_uri=' + redirect_uri;

    $.ajax({
        url: "/authenticate",
        type: 'POST',
        data: JSON.stringify(url),
        contentType: 'application/json',
        dataType: 'json'
    }).done(function (data) {
        console.log(data);
    })

Nothing is printed in the console. Can you tell me what the problem is please ?

Gustavo
  • 3,461
  • 7
  • 26
  • 41

2 Answers2

1

It turns out that the string that I return from the controller cannot be parsed to json because it doesn't have a valid format

I changed the line

return new ResponseEntity<>("TOKEN", HttpStatus.OK);

to

return new ResponseEntity<>("\"TOKEN\"", HttpStatus.OK);

And it works fine.

Return JSON for ResponseEntity<String>

Gustavo
  • 3,461
  • 7
  • 26
  • 41
0

@Gustavo

Hi, have you tried JSON view? JSON always returns a key value; by which we can access elements. Mostly the key value is suffix with 'Result' along with Funcation name.

If your looking at JSON.stringfy("..") is used to just check if data received is correct, after hat you need to paese J

var text = JSON.parse(response.data.GetAllDataResult);

Geeking
  • 1
  • 2