简体   繁体   中英

why i am getting empty response text in ajax?

below is my code fo ajax which sends a string data via post method, the request is successful but I get an empty response. I had checked the readystate and status both are proper and the php file is in the same directory.

function getData(str)
        {   
            if (str == "")
            {
            } else
            {
                if (window.XMLHttpRequest)
                {   

                   var dat = new XMLHttpRequest();
                } else
                {   

                    dat = new ActiveXObject("Microsoft.XMLHTTP");
                }
                 dat.open("POST","userdat.php",true);

                dat.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

                dat.onreadystatechange = function ()
                {    
                    if (dat.readyState == 4 && dat.status == 200)
                    {   
                         alert(dat.responseText);
                         $('#dataReT').text(dat.responseText);
                    }
                }

                dat.send("userid=" + str);

            }

        }

content of my php file:

<?php
$id=$_REQUEST['userid'];
echo $id;
?>

there is no userid in $_REQUEST. Try to add this to your php file:

if(array_key_exists('userid',$_REQUEST)) { 
   echo $_REQUEST['userid']; 
} else { 
   echo 'no userid.';
}

You may send userid value from your javascript file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM