AJAX返回null? [英] AJAX returns null?

查看:140
本文介绍了AJAX返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax从服务器获取信息.页面xxx.php是仅写入123的页面.但是,当我要返回该页面的内容时,它将返回null.

I am trying to use ajax to get information from my server. the page xxx.php is a page with only 123 written on it. However, when I want to return the content of that page, it returns null.

function myFunct(){
    $.ajax({
        url: 'xxx.php',
        success: function(data) {
            return data;
        }
    });
}

var data = myFunct(); //nothing.

推荐答案

请注意,ajax是异步的".因此,在myFunct()完成执行时,可能尚未收到对服务器调用的响应. 您可以将处理来自服务器调用的数据的逻辑放在ajax的成功"中.

Please note that ajax is 'asynchronous'. So, the response to your server call may not received by the time myFunct() completes execution. You may put the logic of process the data from your server call in the 'success' of ajax.

function myFunct(){
    $.ajax({
        url: 'xxx.php',
        success: function(data) {
           // processMyData(data);
        }
    });
}

这篇关于AJAX返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆