Javascript jquery ajax递归调用
我有这样的代码。我试着做递归回调函数的工作。当我运行这个程序时,我得到了来自ajax的第一个响应,在“nextWord”一栏中,我得到了第二个词。但我不能通过回调doWork函数来获得第二个响应 你有什么建议吗Javascript jquery ajax递归调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有这样的代码。我试着做递归回调函数的工作。当我运行这个程序时,我得到了来自ajax的第一个响应,在“nextWord”一栏中,我得到了第二个词。但我不能通过回调doWork函数来获得第二个响应 你有什么建议吗 <script> function doWork() { $.ajax({ type: "POST", url: "eu.php", data: { word: $('#next
<script>
function doWork() {
$.ajax({
type: "POST",
url: "eu.php",
data: { word: $('#nextWord').html()},
dataType: 'json',
success: function( msg ) {
alert($('#nextWord').html());
$( "#nextWord" ).html( msg.nextWord );
$( "#query" ).html( msg.query );
doWork();
//doWork; --- I try and this
},
});
}
$(function() {
doWork();
});
</script>
<div id="nextWord">firstword</div>
<div id="query"></div>
函数doWork(){
$.ajax({
类型:“POST”,
url:“eu.php”,
数据:{word:$('#nextWord').html()},
数据类型:“json”,
成功:功能(msg){
警报($('#nextWord').html());
$(“#nextWord”).html(msg.nextWord);
$(“#查询”).html(msg.query);
销钉();
//道克;---我试着把这个
},
});
}
$(函数(){
销钉();
});
第一个字
您应该使用.then而不是success回调来避免为同一事物使用两个处理程序
<script>
function doWork() {
$.ajax({
type: "POST",
url: "eu.php",
data: { word: $('#nextWord').html()},
dataType: 'json',
success: function( msg ) {
alert($('#nextWord').html());
$( "#nextWord" ).html( msg.nextWord );
$( "#query" ).html( msg.query );
},
}).then(function(data){
doWork();
});
}
$(function() {
doWork();
});
函数doWork(){
$.ajax({
类型:“POST”,
url:“eu.php”,
数据:{word:$('#nextWord').html()},
数据类型:“json”,
成功:功能(msg){
警报($('#nextWord').html());
$(“#nextWord”).html(msg.nextWord);
$(“#查询”).html(msg.query);
},
}).then(功能(数据){
销钉();
});
}
$(函数(){
销钉();
});
可能存在的副本