首页 > Web开发 > 详细

ajax return 的问题

时间:2019-04-05 00:11:54      阅读:173      评论:0      收藏:0      [点我收藏+]

平时都是在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;
    }

 

ajax return 的问题

原文:https://www.cnblogs.com/7qin/p/10657644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!