在发布状态变化之前执行AJAX onreadystatechange? [英] AJAX onreadystatechange executing before post state change?

查看:51
本文介绍了在发布状态变化之前执行AJAX onreadystatechange?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我一直试图让它工作几个小时....

Ok, so I've been trying to get this to work for a couple hours now....

这是我简单的ajax请求:

Here is my simple ajax request:

function ajaxRequest()
{

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;

}

}

然后我有这个函数调用它:

Then I have this function calling it:

函数run_ajax_form_submit_request(s_method,s_processor,s_params,b_syn)
{

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

var s_result = run_ajax_form_submit_request('POST', s_path, s_send_parameters, true);

! !结束代码!!!

!!! END CODE !!!

正在执行的是代码正确执行,直到它到达此行:
request.open(POST,。/ scripts / pages / index / index / form_processor.php?,true);

What's happening is the code is executing correctly until it gets to this line: request.open("POST", "./scripts/pages/index/index/form_processor.php?", true);

然后它运行onreadystatechange函数,由于还没有运行,它返回错误1.消息。

Then it's running the onreadystatechange function which, since nothing has run yet, is returning the "Error 1." message.

然后邮件脚本的其余部分继续并再次更改状态,这次运行4,但onreadystatechange无法再次运行,从而产生最终消息错误1。...代码将在调试器中正常运行,但无法在浏览器中运行...以下是调用其他所有内容的javascript的第一页:

Then the rest of the post script continues and changes the state again, this time it runs 4, but the onreadystatechange fails to run again, resulting in a final message of "Error 1."... The code will run properly in a debugger, but fails to run in the browser... Here is the first page of javascript that calls everything else:

!!! INITIAL CODE !!!

!!! INITIAL CODE !!!

function submit_post_form(s_form_name,s_path,b_restrict,s_success_response)
{

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

}

我不知道我做错了...也许是超时(动画),但我不知道....

I don't know what I'm doing wrong... maybe it's the timeouts (for animation), but I have no idea....

推荐答案

这就是它的工作方式, onreadystate 事件在请求期间多次触发,在发送任何内容之前的第一个事件,这就是为什么你必须听取数字 4 ,您出于某种原因假设 1 2 等。是错误,它们不是,它们只是请求不同阶段的状态代码。

That's the way it works, the onreadystate event fires several times during the request, the first before anything is even sent, that's why you have to listen for the number 4, you're for some reason assuming 1, 2 etc. are errors, they aren't, they are just statuscodes for different stages of the request.

状态代码如下:

0:请求未初始化

1:已建立服务器连接

2:收到请求

3:处理请求

4:请求已完成且响应已准备好

0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready

您知道为什么 1 正在解雇,这不是错误代码,它是建立连接时的状态代码。

You see why the 1 is firing, it's not an errorcode, it's the statuscode for when the connection is established.

你通过删除您添加的所有错误错误处理来解决此问题。

You solve this by removing all the erroneous error handling you've added.

您的代码还存在另一个问题,您正在以这种方式设置ajax

There's also another issue with your code, you're setting up the ajax this way

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

你打电话就像

var s_result = run_ajax_form_submit_request('POST', s_path, s_send_parameters, true);

但由于ajax是异步,因此无法正常工作,请参阅此答案详细解释

but that's never going to work because ajax is asynchronous, see this answer for a detailed explanation

如何从Ajax通话中返回响应

这篇关于在发布状态变化之前执行AJAX onreadystatechange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆