Trying to use JSONP to retrieve JSON from a different domain that utilizes Spring. I have implemented JsonpControllerAdvice
:
@ControllerAdvice
public class JsonpControllerAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpControllerAdvice() {
super("callback");
}
}
The controller looks like this:
@RequestMapping("games")
@RestController
public class GameController extends BaseGameController {
private static final Logger LOGGER = Logger.getLogger(GameController.class);
private static final String KAFKA_TOPIC = "virto_games";
private static final String ROOT_URL = ServerConfig.GAMES_HOST + "/games";
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> games(
@RequestParam(defaultValue = "0") int page
) {
String url = ROOT_URL + "?page=" + page;
return request.get(url);
}
}
The browser returns the following JSON:
{
"items":[{
"id":"1b4a3104-925b-46ac-a2da-2187bbf46e0a",
"name":"Minecraft",
"description":"Game",
"user_id":"ee430ea5-0977-4fc2-b1c4-cc33ddd9db56"
}],
"number":0,
"count":1
}
Below is my JavaScript code attempting to retrieve the JSON:
var virtoHost = "http://localhost:8080/";
function getGames() {
$.ajax({
url: virtoHost + 'games',
dataType: 'JSONP',
jsonpCallback: 'callback',
type: 'GET',
success: function (data) {
alert(JSON.stringify(data));
}
});
}
However, it crashes with an exception:
SyntaxError: unexpected token: ':'
[Learn More]
games:1:8
What could be causing this issue? The JSON format appears correct and ':' is a standard delimiter.