drtkyykai004574380 2018-09-15 17:44
浏览 40

responsiveText返回“未定义”值

I am using JavaScript Ajax to get response from PHP. In PHP I used echo to return the value for responsiveText in Ajax however on console.log I see response as undefined and therefore further operations are not possible.

AJAX request is:

var xmlhttp = new XMLHttpRequest();
var senddata = "c=" + document.getElementById("image_value").value; 
xmlhttp.open("POST", "image_index.php",true);

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

xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
    console.log(xmlhttp.responsiveText);    
    }   
};
xmlhttp.send(senddata);

PHP file is:

<?php
$im[] = "value1";
$im[] = "value2";
$im[] = "value3";

$i=-1;
if(isset($_POST['i_value'])){
    $i=$_POST['i_value'];
}
if($i==-1){
    $u="NOTHING";
}
else{
    $u=$im[$i];
}
echo "$u";
?>

where im is an array

  • 写回答

1条回答 默认 最新

  • douzhan1031 2018-09-15 17:57
    关注

    Don't fret, it's just a simple typo. The response text is responseText not responsiveText.

    I would also recommend using camel case for your variables, for good programming practice. (e.g. dataToSend instead of senddata) In addition, since you used this inside xmlhttp.onreadystatechange I would recommend using this to access its responseText property. See code sample to see what I mean.

    var xmlhttp = new XMLHttpRequest();
    var dataToSend = "c=" + document.getElementById("image_value").value; 
    xmlhttp.open("POST", "image_index.php",true);
    
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        console.log(this.responseText);    
      }   
    };
    xmlhttp.send(dataToSend);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器