Javascript 使用xmlhttp请求从ajax调用中获取值
我使用post方法提出请求。我无法找到响应文本的值。可能是我缺乏获得回复文本的知识。如果数据库中存在数据,我试图告诉用户,他可以发布数据,否则他将得到一个警告,他需要更改名称。 以下是我的ajax调用:- 功能检查新增(大学名称、学期号、部门名称、uid、年份) {Javascript 使用xmlhttp请求从ajax调用中获取值,javascript,ajax,codeigniter,xmlhttprequest,httpresponse,Javascript,Ajax,Codeigniter,Xmlhttprequest,Httpresponse,我使用post方法提出请求。我无法找到响应文本的值。可能是我缺乏获得回复文本的知识。如果数据库中存在数据,我试图告诉用户,他可以发布数据,否则他将得到一个警告,他需要更改名称。 以下是我的ajax调用:- 功能检查新增(大学名称、学期号、部门名称、uid、年份) { var getName=document.getElementById("add_new_name").value; var getTitle=document.getElementById("add_n
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);
}
}
变量
http
未定义,应为xmlhttp
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
//or
//alert(this.responseText);
location.reload();
}
}
您有
alert(http.responseText);
但您从未定义http
。您的对象被称为xmlhttp
。您应该检查jquerydon't understand@geedubbjquery是一个javascript库,其中包含用于删除XmlHttpRequest的函数。它可能值得一看