首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取XMLhttp responseText?

如何获取XMLhttp responseText?
EN

Stack Overflow用户
提问于 2012-10-10 07:05:38
回答 1查看 478关注 0票数 1

我不太熟悉xmlhttp对象,尽管有可用的文档,但我在获取responseText时遇到了问题。我离你这么近了,但如果有人能指出为什么下面的代码不能工作,我会很感激的;在send()之后,它就死了……

代码语言:javascript
复制
function fetchgenotype() {
    // here's where we use an Ajax function to fetch the allele values without reloading the page.
    // Get the index number of the genotype record to retrieve; retrieve it; populate the alleles.
    var mndx, sndx = 0;
    mndx = $('#marker').val();
    if (Study_selected) {
        sndx = $('#stylabid').val();
    } else {
        sndx = $('#pjtlabid').val();
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            // Recieve the return value, parse and assign to screen form. 
            var allele_stg = "";
            allele_stg = document.getElementById("allele_ary").innerHTML = xmlhttp.responseText;
            ary = new Array();
            ary = str_getcsv(allele_ary, ',');
            $('#allele_1_1').val() = ary[0];
            $('#allele_1_2').val() = ary[1];
        }
    }
    xmlhttp.open("GET", "fetchAlleles.php?mndx=" + mndx + "&sndx=" + sndx, true);
    xmlhttp.send();
} // end of fetchgenotype
EN

回答 1

Stack Overflow用户

发布于 2012-10-10 07:35:39

您可以通过将if语句更改为:

代码语言:javascript
复制
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    // Receive the return value, parse and assign to screen form. 
    var allele_stg = "",
        rText = xmlhttp.responseText;
    /*
     * By assigning xmlhttp.responseText to a seperate variable
     * you can now check to make sure rText contains what you
     * expect to see.
     * 
     * By making the call to getElementById and assigning the
     * innerHTML property in separate statements, you can now
     * check to make sure that getElementById is returning a
     * DOM element, or if it's undefined.
     */
    allele_stg = document.getElementById("allele_ary");
    allele_stg.innerHTML = rText;
    ary = new Array();
    ary = str_getcsv(allele_ary, ',');
    $('#allele_1_1').val() = ary[0];
    $('#allele_1_2').val() = ary[1];
} else {
    console.log('xmlhttp.readyState:' + xmlhttp.readyState);
    console.log('xmlhttp.status:' + xmlhttp.status);
}

然后监控控制台。我怀疑你要么是得不到200 xmlhttp.status,要么是xmlhttp.readyState永远不会访问4

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12809680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档