简体   繁体   English

Ajax在codeigniter中无法正常工作

[英]Ajax is not working properly in codeigniter

function get_caste()
  { 

    var ajaxRequest;  // The variable that makes Ajax possible!
    var course_id = document.getElementById('Course_id').value;
    var Entry_Type = document.getElementById('Mode_Of_Adm').value;
    if(course_id=="")
    {

    }
    else{


      ajaxRequest = new XMLHttpRequest();
      ajaxRequest.onreadystatechange = function()
      {
        if(ajaxRequest.readyState == 4)
        {
          var ajaxDisplay = document.getElementById('Caste_marks');
          ajaxDisplay.innerHTML = ajaxRequest.responseText;    
        }
      }
      ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" +Entry_Type'/' +course_id true);
     ajaxRequest.send(); 
     }

  }

</script>

the above Js code fetching cast from getcastebycourseentry function but when i Run This code without ajaxRequest The Code are: 上面的Js代码是从getcastebycourseentry函数中获取的,但是当我运行时,此代码没有ajaxRequest的代码是:

   ajaxRequest = new XMLHttpRequest();
   ajaxRequest.onreadystatechange = function()
   {
    if(ajaxRequest.readyState == 4)
    {
      var ajaxDisplay = document.getElementById('Caste_marks');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;    
    }
  }
  ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" +Entry_Type'/' +course_id true);
 ajaxRequest.send(); 

its works fine. 它的作品很好。 but when i put above ajaxRequest on it its not working?!! 但是当我把上面的ajaxRequest放在上面时,它不起作用吗?!! get_caste() Function not working? get_caste()函数不起作用?

try this will work for you.

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function get_caste(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    var course_id = document.getElementById('Course_id').value;
    var Entry_Type = document.getElementById('Mode_Of_Adm').value;

    var queryString = "?Course_id=" + Course_id + "&Mode_Of_Adm=" + Mode_Of_Adm;
    ajaxRequest.open("GET", "<?php echo base_url();?>registrar/AddStudentManually/getcastebycourseentry/" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
Max Age: <input type='text' id='Course_id' /> <br />
Max WPM: <input type='text' id='Mode_Of_Adm' />
<br />

<input type='button' onclick='get_caste()' value='Query MySQL' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM