好的,所以我一直试图让它工作几个小时......
这是我简单的ajax请求:
function 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;
}
}
然后我有这个函数调用它:
function 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;
}
由本声明调用:
var 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的第一页:
!!!初始代码!!!
function 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);
}
我不知道我做错了什么......也许是超时(动画片),但我不知道......
答案 0 :(得分:3)
这就是它的工作方式,onreadystate
事件在请求期间多次触发,第一次事件甚至发送之前,这就是为什么你必须听取数字{{ 1}},您出于某种原因假设4
,1
等是错误,它们不是,它们只是请求不同阶段的状态代码。
状态代码如下
0:请求未初始化
1:建立服务器连接
2:收到请求
3:处理请求
4:请求已完成且响应已准备好
您知道为什么2
正在解雇,它不是错误代码,它是建立连接时的状态代码。
您可以通过删除已添加的所有错误错误处理来解决此问题。
您的代码还存在另一个问题,您可以通过这种方式设置ajax
1
并且您将其称为
function 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) {
message = request.responseText;
}
}
return message;
}
但由于ajax是异步,因此无法正常工作,请参阅此答案以获取详细说明