好了,我一直在努力得到這個現在幾個小時工作....AJAX onreadystatechange在post狀態更改之前執行?
這裏是我的簡單的Ajax請求:
功能ajaxRequest(){
var xmlhttp;
var activexmodes = ["Msxm12.XMLHTTP", "Microsoft.XMLHTTP"];
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
for(var i = 0; i < activexmodes.length; i++)
{
try
{
return new ActiveXObject(activexmodes[i]);
}
catch (e)
{
// Suppress
}
}
}
else
{
return false;
}
}
然後,我有這個函數調用它:
功能run_ajax_form_submit_request(s_method, s_processor,s_params,b_syn) {
var request = new ajaxRequest();
var message = null;
request.onreadystatechange=function()
{
if (request.readyState == 4)
{
if (request.status == 200 || window.location.href.indexOf("http") == -1)
{
if (request.responseText == 'true')
{
message = 'true';
}
else
{
message = request.responseText;
}
}
else
{
messge = "There was a problem processing this request.";
}
}
else if (request.readyState == 0)
{
message = "Error: 0.";
}
else if (request.readyState == 1)
{
message = "Error: 1.";
}
else if (request.readyState == 2)
{
message = "Error: 2.";
}
else if (request.readyState == 3)
{
message = "Error: 3.";
}
}
switch (s_method)
{
case 'POST':
request.open("POST", "./scripts/pages/index/index/form_processor.php?", true);
request.setRequestHeader("Content-s_input_type", "application/x-www-form-urlencoded");
request.send(s_params);
break;
case 'GET':
request.open(s_method, s_processor, b_syn, true);
request.send();
break;
}
return message;
}
正在由該語句稱爲:
變種s_result = run_ajax_form_submit_request( 'POST',S_PATH,s_send_parameters,TRUE);
!!!結束代碼!!!
發生什麼事是代碼正確執行,直到它到達這一行: request.open(「POST」,「./scripts/pages/index/index/form_processor.php?」,true);
然後它運行onreadystatechange函數,由於沒有任何運行,它正在返回「錯誤1」。信息。
然後剩下的post腳本繼續並再次更改狀態,這次它運行4次,但onreadystatechange失敗再次運行,導致最終消息「Error 1。」...代碼將運行在調試器中正確運行,但無法在瀏覽器中運行...以下是JavaScript的第一頁,它調用了其他所有內容:
!!!初始代碼!!!
功能submit_post_form(s_form_name,S_PATH,b_restrict,s_success_response) {
var o_form = document.getElementById(s_form_name);
var oa_inputs = o_form.getElementsByTagName('input');
var oa_selects = o_form.getElementsByTagName('select');
var o_submit = form_find_submit(oa_inputs);
var o_messages = o_form.getElementsByClassName('messages')[0];
o_messages.className = 'messages';
switch (b_restrict)
{
case true:
for (i = 0; i < oa_inputs.length; i++)
{
oa_inputs[i].disabled = true;
}
for (i = 0; i < oa_selects.length; i++)
{
oa_selects[i].disabled = true;
}
break;
}
o_messages.classList.add('loading');
setTimeout(function(){
o_messages.classList.add('active');
setTimeout(function(){
var sa_all_inputs_as_strings = new Array();
var inputIDArray = new Array();
var selectIDArray = new Array();
var inputValArray = new Array();
var selectValArray = new Array();
var s_input_type = null;
for (i = 0; i < oa_inputs.length; i++)
{
s_input_type = oa_inputs[i].type;
var s_input_val_temp = null;
if (s_input_type.trim() != 'button' && s_input_type.trim() != 'submit')
{
sa_all_inputs_as_strings[i] = new Array();
sa_all_inputs_as_strings[i][0] = oa_inputs[i].getAttribute('name');
if (s_input_type == 'text' || s_input_type == 'password')
{
s_input_val_temp = oa_inputs[i].value;
}
else if (s_input_type == 'radio' || s_input_type == 'checkbox')
{
if (oa_inputs[i].checked)
{
s_input_val_temp = 'true';
}
else
{
s_input_val_temp = 'false';
}
}
sa_all_inputs_as_strings[i][1] = s_input_val_temp;
}
}
for (i = sa_all_inputs_as_strings.length; i < (sa_all_inputs_as_strings.length + oa_selects.length); ++i)
{
sa_all_inputs_as_strings[i] = new Array();
sa_all_inputs_as_strings[i][0] = oa_selects.id;
sa_all_inputs_as_strings[i][1] = oa_selects.options[(oa_selects[selectInterval]).selectedIndex].value;
}
var s_send_parameters = "form=" + s_form_name;
for (i = 0; i < sa_all_inputs_as_strings.length; i++)
{
s_send_parameters += "&" + encodeURIComponent(sa_all_inputs_as_strings[i][0]) + "=" + encodeURIComponent(base64Encode(sa_all_inputs_as_strings[i][1]));
}
var s_result = run_ajax_form_submit_request('POST', s_path, s_send_parameters, true);
o_messages.classList.remove('active');
setTimeout(function(){
o_messages.className = 'messages';
if (s_result == 'true')
{
o_messages.innerHTML = s_success_response;
o_messages.classList.add('success');
}
else
{
o_messages.innerHTML = s_result;
o_messages.classList.add('error');
for(i = 0; i < oa_inputs.length; i++)
{
if (oa_inputs[i].type != 'submit')
{
oa_inputs[i].disabled=false;
oa_inputs[i].classList.remove('disabled');
}
}
for (i = 0; i< oa_selects.length; i++)
{
oa_selects[i].disabled=false;
oa_selects[i].classList.remove('disabled');
}
}
setTimeout(function(){
o_messages.classList.add('active');
}, 100);
}, 200);
},200);
},100);
}
我不知道我做錯了......也許是超時(動畫),但我不知道....
好吧,我拿出了所有的錯誤信息,但現在它根本沒有返回任何東西......調試器仍然正確,但瀏覽器仍然失敗...還有什麼錯誤? – Jingrah1
是的,ajax調用是** async **,你不能這樣做。 – adeneo
謝謝。我非常感激,我會在這裏花更長的時間來試圖解決這個問題。 – Jingrah1