【问题标题】:Getting value from ajax calling using xmlhttp request使用 xmlhttprequest 从 ajax 调用中获取价值
【发布时间】:2013-11-17 15:50:44
【问题描述】:

我正在使用 post 方法发出请求。我无法找到响应文本的值。可能是我缺乏有关如何获取响应文本的知识。如果数据库中存在数据,我试图告诉用户他能够发布数据,否则他会收到需要更改名称的警报。 这是我的ajax调用:- 函数 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);


        }




    }

【问题讨论】:

  • 你应该看看 jquery
  • 不懂@geedubb
  • jquery 是一个 javascript 库,其中包含抽象出 XmlHttpRequest 的函数。可能值得一看jquery.com

标签: javascript ajax codeigniter xmlhttprequest httpresponse


【解决方案1】:

变量http没有定义,应该是xmlhttp

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多