dtbrd80422 2010-04-13 16:20
浏览 43

jQuery在继续[重复]之前等待ajax调用结束

This question already has an answer here:

I have a problem with a jQuery ajax call. I need to wait for the call to be finished in order to return a value. However, the script seems to jump ahead and not wait for the call to be over. my function then returns "undefined".

I have tried to use the .ajax() method and set the value of async to false, but this would not work either.

I could I get my function to return the value that I could through the ajax call?

Thank you!

Here is the code:

function get_rsrce_name(){

    jQuery.post(

        '<?php echo admin_url( 'admin-ajax.php' ); ?>',

        { action :'my_action', option_rsrce : 'option_rsrce' },

        function( data ) {

            output =  data.option_name;         

        },

        "json"
    );      

    return output;
}
</div>
  • 写回答

4条回答 默认 最新

  • douwei9973 2010-04-13 16:30
    关注

    The reason it isn't working when you tell it to run synchronously is that the variable 'output' is only defined within the scope of the callback function.

    There is almost certainly a better way to do what you're trying to do, but without more context, it'll be tough to explain what that might be.

    I'd recommend reading up on "javascript variable scope"

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题