AJAX Ready State坚持1

时间:2011-10-06 16:21:20

标签: ajax readystate

嗨我可以看到这已被讨论但在仔细阅读问题/答案后,我似乎仍然无法通过这个简单的AJAX调用来突破已准备好的状态1。

这是我的Javascript:

<script language="javascript" type="text/javascript">
var request;

function createRequest()
{
   try 
   {
      request = new XMLHttpRequest();
   }   catch (trymicrosoft) {
   try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (othermicrosoft) {
      try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = false;
         }
     }
 }
if (!request)
   alert("Error initializing XMLHttpRequest!");
}
function loadClassesBySchool()
{    
     //get require web form pieces for this call
 createRequest(); // function to get xmlhttp object
 var schoolId = getDDLSelectionValue("ddlSchools");   
 var grade = getDDLSelectionValue("ddlGrades");

     var url = "courses.php?grades=" + escape(grade) + "&schoolId=" + escape(schoolId);

 //open server connection
 request.open("GET", url, true);

 //Setup callback function for server response

   //+++read on overflow that some fixed the issue with an onload event this simply had
   //+++the handle spitback 2 readystate = 1 alerts

     request.onload = updateCourses();

     request.onreadystatechanged = updateCourses();

 //send the result
 request.send(); 
}

function updateCourses()
{

    alert('ready state changed' + request.readyState);

}

function getDDLSelectionValue(ddlID)
{
    return    document.getElementById(ddlID).options[document.getElementById(ddlID).selectedIndex].value;   
  }

</script>

PHP在这里只是一个简单的打印,如果我导航到浏览器(IE / Chrome)加载正常:

<?php
    print "test";
?>

我对此非常陌生,但似乎我无法获得最流行的AJAX调用工作,任何有关如何通过此工作的帮助将不胜感激。

我退出回调函数'updateCourses'只需1 ...

1 个答案:

答案 0 :(得分:0)

经过多次挖掘后,我实际上已经放弃并切换到jQuery,除了jQuery的工作原理之外,所有意图和目的都应该做同样的事情......我对它不太满意但是就这样了。

这是完成相同的jQuery:

function loadCoursesBySchool(){

    var grades = getDDLSelectionValue("ddlGrades");
    var schoolId = getDDLSelectionValue("ddlSchools");

    jQuery.ajax({   
        url: "courses.php?grades=" + grades + "&schoolId=" + schoolId,  
        success: function (data) {  
            courseDisplay(data);    
        }   
    }); 
}

function courseDisplay(response)
{
    //check if anything was setn back!?
    if(!response)
    {
        $("#ddlCourses").html("");
        //do nothing?   
    }
    else
    {
        //empty DLL
        $("#ddlCourses").html("");
        //add entries
        $(response).appendTo("#ddlCourses");
    }       
}