問題描述
我正在努力為這個項目學習 AJAX.我有一個網站加載患者正在服用的藥物.
I am trying to learn AJAX for this project at work. I have a site that loads medications that a patient is taking.
我遞歸地調用這個 AJAX 函數,以便它會附加一個新表,其中包含一種藥物和 7 天的歷史記錄.我在讓代碼在 FF 和 IE 中執行時遇到問題.在鍍鉻中工作得很好.我有顯示 xmlhttp.status 的警報,這就是我得到的:
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(內部服務器錯誤).
xmlhttp.status==500 (internal server error).
我注釋掉了我所有的遞歸,所以它被縮小到這個小代碼.(x 跟蹤藥物的數量,所以我知道什么時候停止)
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.
推薦答案
500 (internal server error)
表示服務器端出現問題.這可能是幾件事,但我會首先驗證 URL 和參數是否正確.另外,請確保處理請求的任何東西都期望請求是 GET 而不是 POST.
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.
了解更多信息的一種有用方法是使用 Fiddler 之類的工具讓您查看所有 HTTP 請求和響應,以便準確查看您發送的內容以及服務器的響應.
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.
如果您沒有令人信服的理由來編寫自己的 Ajax 代碼,那么最好使用一個為您處理 Ajax 交互的庫.jQuery 是一種選擇.
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.
這篇關于Ajax - 500 內部服務器錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!