weixin_33744141 2016-04-03 14:05 采纳率: 0%
浏览 204

从Ajax请求返回数据[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-04-03 14:18:16Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

I recover data from a Requette in ajax. I want to use this data in a function and return it but when I call the function it returns nothing

function getdata(nom_produit) {

    $.ajax({
        type: "POST",
        url: "traitement/panne/test_panne.php",
        data: {
            nom_produit: nom_produit
        },

        success: function(data) {
            var obj = jQuery.parseJSON(data);

            jQuery.each(obj["resultat"], function(index, value) {

            })
        }
    });
    return obj;
}

how i can return the data?

</div>
  • 写回答

1条回答 默认 最新

  • 七度&光 2016-04-03 14:13
    关注

    you can't return from like this. $.ajax is an asynchronous call so when return is called your success function is still pending execution. you can do something like below though to achieve same result.

    function getdata(nom_produit, callback) {
    
        $.ajax({
            type: "POST",
            url: "traitement/panne/test_panne.php",
            data: {
                nom_produit: nom_produit
            },
    
            success: callback
        });
    }
    

    and from the place you are calling this function you can do something like below

    var successFunction = function(data) {
        hideOverlay(); // hide overlay once response is there
    
        // your code to process data and show in UI 
    }
    
    showOverlay(); // code to show loading image in UI till response comes
    getData(someId, successFunction);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题