AJAX 就绪状态停留在 1

发布于 2024-12-08 10:11:49 字数 1814 浏览 1 评论 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>

The PHP is HERE just一个简单的打印,如果我在浏览器(IE/Chrome)中导航到该打印,则可以正常加载:

<?php
    print "test";
?>

我对此很陌生,但似乎我无法获得最简单的 AJAX 调用来工作,有关如何工作的任何帮助我们将不胜感激。

我从回调函数“updateCourses”中得到的只是 1...

Hi I can see this has been discussed but after perusing the issues/answers I still don't seem to be able to get even this simple AJAX call to bump out of ready state 1.

Here's the Javascript I have:

<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>

The PHP is HERE just a simple print which if i navigate to in the browser (IE/Chrome) loads fine:

<?php
    print "test";
?>

I'm quite new at this but seems like I can't get the most bare bones AJAX calls to work, any help as to how work past this would be greatly appreciated.

All I get out of my callback function 'updateCourses' is a 1...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽梦忆笙歌 2024-12-15 10:11:49

经过更多的挖掘之后,我实际上放弃了并切换到 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");
    }       
}

Well after more digging I actually gave up and switched over to jQuery which should for all intents and purposes be doing the EXACT same thing except for the fact that jQuery works... I was just less comfortable with it but so be it.

Here's the jQuery to accomplish the same:

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");
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文