0

嗨,我可以看到已经讨论过这个问题,但是在仔细阅读问题/答案之后,我似乎仍然无法通过这个简单的 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...

4

1 回答 1

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");
    }       
}
于 2011-10-07T12:22:54.397 回答