Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

javascript - Ajax - 500 Internal Server Error

I am trying to learn AJAX for this project at work. I have a site that loads medications that a patient is taking.

I call this AJAX function up recursively so that it will append a new table containing a single medication and 7 days worth of history. I am having issues getting the code to execute in FF and IE. Works perfectly fine in chrome. I had alerts displaying the xmlhttp.status and this is what I got:

xmlhttp.status==500 (internal server error).

I commented out all of my recursion so it is narrowed down to this tid bit of code. ( x keeps track of the number of meds so i know when to stop)

function LoadMeds()


  if ( x == MaxMedCount )
  {
      document.getElementById("the_day").value = parseInt(document.getElementById("the_day").value)+7; 
  }
  if ( x == (MaxMedCount - 1) )
  {
      document.getElementById("x").value = x + 1;
      show();
  }
  else
  { 

      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
          try 
          {      
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {     
                  var div = document.createElement('div');
                  div.innerHTML= xmlhttp.responseText;
                  document.getElementById('MedTable').appendChild(div);
                  document.getElementById("the_med_").value = the_med_;

              }

          }
          catch(e)
          {
              alert("Error");  
          }
      }
      xmlhttp.open("GET","URL with variables passed",true);
      xmlhttp.send();     
      document.getElementById("x").value = x + 1;
  } 

if more code is needed just let me know.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The 500 (internal server error) means something went wrong on the server's side. It could be several things, but I would start by verifying that the URL and parameters are correct. Also, make sure that whatever handles the request is expecting the request as a GET and not a POST.

One useful way to learn more about what's going on is to use a tool like Fiddler which will let you watch all HTTP requests and responses so you can see exactly what you're sending and the server is responding with.

If you don't have a compelling reason to write your own Ajax code, you would be far better off using a library that handles the Ajax interactions for you. jQuery is one option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...