微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax return 的问题

平时都是在AJAX里执行逻辑,实然想到能不能return返回数据呢?

ajax 是异步请求,return拿值得时候 ajax并没有取到值,所以是undefind。

需要把ajax的请求方式改为同步 

    var xmlhttp;
    var donestr = loadXMLDoc(‘https://www.cnblogs.com/liudongpei/p/6021170.html‘);

    function loadXMLDoc(url) {
        var htmldata;
        xmlhttp = null;
        if (window.XMLHttpRequest) { // code for IE7,Firefox,Opera,etc.
            xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // code for IE6,IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlhttp != null) {
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) { // 4 = "loaded"
                    if (xmlhttp.status == 200) { // 200 = "OK"
                        htmldata = get_cnblogs_post_body(xmlhttp.responseText);
                    } else {
                        alert("Problem retrieving XML data:" + xmlhttp.statusText);
                    }
                }
            }
            xmlhttp.open("GET",url,false);
            xmlhttp.send(null);
        } else {
            alert("Your browser does not support XMLHTTP.");
        }
        return htmldata;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐