Ajax在codeigniter

时间:2016-06-06 08:59:31

标签: javascript ajax 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>

以上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(); 

它的工作正常。但当我把ajaxRequest放在它上面时它不起作用了?!!  get_caste()函数不工作?

1 个答案:

答案 0 :(得分:0)

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>