Краткий ответ: ваш метод foo()
возвращается немедленно, а вызов $ajax()
выполняется асинхронно после возврата функции . Проблема заключается в том, как и где сохранить результаты, полученные при вызове async, после его возврата.
В этом потоке было задано несколько решений. Возможно, самый простой способ - передать объект методу foo()
и сохранить результаты в члене этого объекта после завершения асинхронного вызова.
function foo(result) {
$.ajax({
url: '...',
success: function(response) {
result.response = response; // Store the async result
}
});
}
var result = { response: null }; // Object to hold the async result
foo(result); // Returns before the async completes
Обратите внимание, что вызов foo()
] все равно не вернут ничего полезного. Однако результат асинхронного вызова теперь будет сохранен в result.response
.
Вы должны обернуть свой скрипт в эту структуру:
<script th:inline="javascript">
/*<![CDATA[*/
$( document ).ready(function() {
for(i=0;i<10;i++) {
...
}
});
/*]]>*/
</script>
EDIT:
Не забудьте сохранить ваши javascript и другие статические файлы в папке /src/main/webapp
ваш проект весенней загрузки
EDIT2:
Вы можете напрямую использовать свой скрипт с тимелеафом:
<tr th:each="i : ${#numbers.sequence( 1, db.columns_number)}">
<td><input id="field" th:name="${'field'+i}" maxlength="255"
required="required" type="text" /></td>
<td><input id="Size" th:name="${'Size'+i}" maxlength="255"
required="required" type="text" /></td>
<td><select id="Type" th:name="${'Type'+i}">
...
</select></td>
<td><select id="null" th:name="${'nullabilite'+i}">
<option value="null">null</option>
<option value="not_null">not_null</option>
</select></td>
</tr>
В тимелеаф текущие сценарии являются javascript (th: inline = "javascript") и dart (th: inline = "dart").
blockquote>Используйте следующий фрагмент вместо
<script type="text/javascript">
<script th:inline="javascript"> /*<![CDATA[*/ ... //your code here ... /*]]>*/ </script>
<table border='1px'>
<thead>
<th>field</th>
<th>Size</th>
<th>Type</th>
<th>null</th>
</thead>
<tbody> </tbody>
</table>
<script th:inline="javascript">
/*<![CDATA[*/
$(document).ready(function () {
for (var i = 0; i < 10; i++) {
var row=[],r=0;
row[r]="<tr>";
row[++r]='<td>';
row[++r]='<input id="field" type="text" name="field"';
row[++r]=i;
row[++r]='maxlength="255" required="required"/>';
row[++r]='</td><td>';
row[++r]='<input id="Size" type="text" name="Size"';
row[++r]=i;
row[++r]= 'maxlength="255" required="required"/>';
row[++r]= '</td>';
/*
* this more readable
* other td
*
*/
row[++r]='</tr>';
$("tbody").append(row.join(""));
}
});
/*]]>*/
</script>