D8: How to perform ajax call in javascript I'm trying to perform an ajax call from a javascfript file by executing this:

            $.ajax({
                url: '/lunapark/ajax/space-invaders/' + score,
                success: function (jsonData) {
                    window.location.replace(jsonData.redirect);
                }
            });


But I keep getting: 


  GET /lunapark/ajax/space-invaders/90 404 (Not Found)


If I go to the url in my google chrome, it loads and gives me a JSON object, so the page does exist:

{"redirect":"\/lunapark\/overview","game":"space-invaders","score":"90"}


Now I'm trying this:

new Drupal.ajax();


But I can't seem to figure it out...
Instead of specifying an incorrect datatype in the client call, you fix your route and specify that you return json. Use the _format route key as explained in the route documentation