Правильный ajax-скрипт, который получает список объектов из базы данных - PullRequest
0 голосов
/ 08 сентября 2018

Я бы хотел, чтобы ajax получил список ответов страны, получил имена и добавил его в <select> в качестве параметров. Вот мой HTML-код:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>Test</title>
        <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>  
        <select id = "countrySelect" >
            <option disabled>Countries</option>
        </select>
        <script>     
            $.ajax({
                url:"http://localhost:8080/public/getAllCountries",
                type:"GET",
                dataType: 'json',
                contentType:"application/json",
                success: function(countriesList){
                    for(var i = 0; i<countries.length; i++){
                        var countryName = countriesList[i].name;
                        $("#countrySelect").append('<option value = "' + countryName + '">' + countryName + '</option>' ); 
                    } 
                },
                error:function(error){
                    alert("error");
                }
            });    
        </script>
    </body>
</html>

Существует доказательство того, что метод get работает: снимок экрана .

Ответы [ 2 ]

0 голосов
/ 08 сентября 2018

Есть окончательный рабочий код. Надеюсь, это кому-нибудь поможет!

    <!DOCTYPE HTML>
<html>
 <head>
  <meta charset="utf-8">
  <title>Test</title>
   <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 </head>
 <body>  


   <select id = "countrySelect" >
       <option disabled>Countries</option>
   </select>

<script>     
    $.ajax({
            url:"http://localhost:8080/public/getAllCountries",
            type:"GET",
            dataType: 'json',
            contentType:"application/json",
            success: function(countryList){
                for(var i = 0; i<countryList.length; i++){
                    var countryName = countryList[i].name;
                    $("#countrySelect").append('<option value = "' + countryName + '">' + countryName + '</option>' ); 
                } 
            },
            error:function(error){
                alert("error");
            }
        });    
     </script>
 </body>
</html>
0 голосов
/ 08 сентября 2018

Имя вашей переменной countriesList, и вы указали неопределенную переменную contries в цикле for.

Чтобы получить название каждой записи, вы должны использовать var countryName = countriesList[i].name вместо var countryName = countriesList[i].

ID тега select - это 'countrySelect', а не 'myTable'.

Вот исправленный скрипт, он должен работать на вас:

<script>     
    $.ajax({
            url:"http://localhost:8080/public/getAllCountries",
            type:"GET",
            dataType: 'json',
            contentType:"application/json",
            success: function(countriesList){
                for(var i = 0; i<countriesList.length; i++){
                    var countryName = countriesList[i].name;
                    $("#countrySelect").append('<option value = "' + countryName + '">' + countryName + '</option>' ); 
                } 
            },
            error:function(error){
                alert("error");
            }
        });    
</script>

Надеюсь, я подтолкнул тебя дальше.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...