使用xmlhttprequest从ajax调用获取值

时间:2013-11-17 15:50:44

标签: javascript ajax codeigniter xmlhttprequest httpresponse

我正在使用post方法发出请求。我无法找到响应文本的值。可能是我缺乏如何获得响应文本的知识。如果数据库中存在数据,我试图告诉用户他能够发布数据,否则他将收到一个警告,他需要更改名称。 这是我的ajax电话: - function checkAddedNew(univ_name,term_no,dept_name,uid,year)             {

    var getName=document.getElementById("add_new_name").value;      
    var getTitle=document.getElementById("add_new_title").value;    

    var hasSpace=getTitle.indexOf(' ');


    if(hasSpace >= 0)
    {
        alert("Please fill up the title without space");
    }
    else
    {
        if(getName&& getTitle)
        {               
            var r=confirm("Do you want to add?");
        }
        else if(!getName && getTitle)
        {
            alert("Please fill up the name");
        }
        else if(getName && !getTitle)
        {
            alert("Please fill up the title");
        }
        else 
        {
            alert("Please fill up the form.");
        }


    if (r==true )
        {
            var xmlhttp;    


            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)
                {

                alert(http.responseText);
                location.reload();
                }
              }

            xmlhttp.open("POST","../addnew/check",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");                   
            xmlhttp.send("title="+getTitle+"&name="+getCourseName+"&name="+univ_name+"&term_no="+term_no+"&department_name="+dept_name+"&uid="+uid+"&year="+year);
        }

    }

}

我在这里收到回复xmlhttp.readyState == 4&& xmlhttp.status == 200。这是我的控制器: -

function check()
    {


        $title = $this->input->post('title');
        $name=$this->input->post('name');   
        $university_name=$this->input->post('university_name');
        $term_no=$this->input->post('term_no');
        $department_name=$this->input->post('department_name');
        $uid = $this->input->post('uid');
        $year=$this->input->post('year');   



        $CI =& get_instance();                  
        $log_username=$CI->session->userdata('username');   

        $now = time();
        $human = unix_to_human($now);

        $this->load->model('model');
        $isUnique=$this->model->checkNew($name,$title,$log_username,$human,$term_no,$university_name,$year,$department_name,$uid);      


        if($isUnique)
        {

            $this->course_model->insertNewCourse($course_name,$course_title,$log_username,$human,$term_no,$university_name,$year,$department_name,$uid);


        }




    }

2 个答案:

答案 0 :(得分:0)

未定义变量http,它应为xmlhttp

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    alert(xmlhttp.responseText);
    //or
    //alert(this.responseText);
    location.reload();
    }
  }

答案 1 :(得分:0)

您有alert(http.responseText);,但您从未定义http。您的对象名为xmlhttp