0

I started learning AJAX recently and am trying a very simple project which involves capturing some form data and sending it to two servers.

The first server is the one which hosts the website and server side php handling. This worls fine

The second server is a python basic http server which handles only the POST operation request send from AJAX. This functionality works but is a bit weird.

Let me explain

Here is my AJAX code which is absolutely straight forward.

function xml_http_post(url, data) {
    var req = false;
    try {
        // Firefox, Opera 8.0+, Safari
        req = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
    //        callback(req);

        }
    }

    req.open("POST", url, true);
    req.setRequestHeader("Content-type","text/plain");
    req.send(data);
}  

Since I do not intend to send back any response , my callback function on ready state change is empty.

But when I execute this code ( triggered by onclick on a button) , the POST doesnt work and server doesnt seem to receive anything.

But the most surprising thing is that if I keep a breakpoint at req.open( ) and then do a manual step execution then it works always. Which means , I guess that there is some timing issue which needs to be resolved.

It works fine without breakpoints if the third parameter "async" is set to false but that is anyway undesirable so I want to make it work with async = true.

Any help would be greatly appreciated.

Thanks Shyam

4

1 に答える 1

0

私が理解したように、フォーム ページは最初のサーバーでフォーム b のアクションとして呼び出された php スクリプトによってアンロードされていました。これにより、javascript コードが部分的に実行されるか、実行されませんでした。

だから私は同期XHRが私の唯一の方法であることを理解しました.

于 2012-10-27T19:39:11.943 に答える