I make an ajax request to the API, but instead of the json object, undefined returned:

 return JSON.parse($.ajax({ url: '//autocomplete.travelpayouts.com/jravia?locale=ru&with_countries=false&q=' + $('#takeoff_place').val(), type: 'GET', dataType: 'json' }).responseText); 

What am I doing wrong?

Reported as a duplicate by members of Bald , Grundy , Community Spirit Mar 7 '17 at 14:57 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • At the time of the return execution, the $ .ajax () execution has not finished yet - Sergey Gornostaev

2 answers 2

What is not satisfied with this option?

 $.getJSON('autocomplete.travelpayouts.com/jravia?locale=ru&with_countries=false&q=' + $('#takeoff_place').val(), function( json ) { console.log(json); }); 

     <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $(document).ready(function() { $("#load").click(function() { var settings = { "async": true, "crossDomain": true, "url": "http://autocomplete.travelpayouts.com/jravia?locale=ru&with_countries=false&q="+$("#takeoff_place").val(), "method": "GET" } $.ajax(settings).done(function(response) { console.log(response); }); }) }) </script> </head> <body> <input id="takeoff_place" placeholder="Введите город :)"><br> <a id="load" href="#">Load</a> </body> </html>