Home  >  Article  >  Backend Development  >  Why does TP show as undefined after submitting via ajax?

Why does TP show as undefined after submitting via ajax?

WBOY
WBOYOriginal
2016-12-01 00:56:491236browse

After submission, the background receives data through $name=I('name') and $pw=I('pw'), but after the echo comes out, it shows undefined. Why is this? The returned value is always 0. Is it the ajax part of the code? But it’s correct when you check it with F12

<code><script type="text/javascript">
       function ajax(url,data,data2,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('post',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            oAjax.send("name="+data+"&pw="+data2);    
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
              }
            }
        }
</script>
<script type="text/javascript">
window.onload=function(){
   var oBtn1=document.getElementById('btn1');
   var oName=document.getElementById('name');
   var oPw=document.getElementById('pw');
   var oDiv5=document.getElementById('div5');
   oBtn1.onclick=function(){
      oV1=oName.value;
      oV2=oPw.value;
      ajax("{:U('login/ajax')}",oV1,oV2,function(str){
        oDiv5.innerHTML=str;
      });
    }
}
</script></code>
<code>public function ajax(){
            $user=M('user');
            $name=I('name');
            $pw=I('pw');
            echo $pw;                      //提示未定义
            echo $name;                    //提示未定义    
            $res1=$user->where("username='$name' AND password='$pw'")->select();
            if($res1){
                $res=1;
            }else{
                $res=0;
            }
        $this->ajaxReturn($res);
    }</code>

Reply content:

After submission, the background receives data through $name=I('name') and $pw=I('pw'), but when the echo comes out, it shows undefined. Why is this? The returned value is always 0. Is it the ajax part of the code? But it’s correct when you check it with F12

<code><script type="text/javascript">
       function ajax(url,data,data2,funsucc){
            var oAjax=new XMLHttpRequest();
            oAjax.open('post',url,true);                   
            oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            oAjax.send("name="+data+"&pw="+data2);    
            oAjax.onreadystatechange=function(){
              if(oAjax.readyState==4){
                if(oAjax.status==200){
                  funsucc(oAjax.responseText);
                }
              }
            }
        }
</script>
<script type="text/javascript">
window.onload=function(){
   var oBtn1=document.getElementById('btn1');
   var oName=document.getElementById('name');
   var oPw=document.getElementById('pw');
   var oDiv5=document.getElementById('div5');
   oBtn1.onclick=function(){
      oV1=oName.value;
      oV2=oPw.value;
      ajax("{:U('login/ajax')}",oV1,oV2,function(str){
        oDiv5.innerHTML=str;
      });
    }
}
</script></code>
<code>public function ajax(){
            $user=M('user');
            $name=I('name');
            $pw=I('pw');
            echo $pw;                      //提示未定义
            echo $name;                    //提示未定义    
            $res1=$user->where("username='$name' AND password='$pw'")->select();
            if($res1){
                $res=1;
            }else{
                $res=0;
            }
        $this->ajaxReturn($res);
    }</code>

<code>  var oName=document.getElementById('name');
   var oPw=document.getElementById('pw');
</code>

Try out these two data and take a look. If no object assignment is found, it will be undefined;

undefined must be a js problem, please post the detailed error

Check the data in the submission form

ajax("{:U('login/ajax')}",oV1,oV2,function(str){ });

<code>  你这个地方只是把值传进去了,没有定义 name,pw.</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn