Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery ajax递归调用_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript jquery ajax递归调用

Javascript jquery ajax递归调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有这样的代码。我试着做递归回调函数的工作。当我运行这个程序时,我得到了来自ajax的第一个响应,在“nextWord”一栏中,我得到了第二个词。但我不能通过回调doWork函数来获得第二个响应 你有什么建议吗 <script> function doWork() { $.ajax({ type: "POST", url: "eu.php", data: { word: $('#next

我有这样的代码。我试着做递归回调函数的工作。当我运行这个程序时,我得到了来自ajax的第一个响应,在“nextWord”一栏中,我得到了第二个词。但我不能通过回调doWork函数来获得第二个响应

你有什么建议吗

<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(功能(数据){
销钉();
});
}
$(函数(){
销钉();
});

可能存在的副本