简体   繁体   English

我的JavaScript在Ajax响应后停止工作

[英]My javascript stops working after ajax response

For some reason my javascript stops working after I have submitted a POST request. 由于某种原因,我的javascript在我提交POST请求后停止工作。

Here is what is happening. 这是正在发生的事情。 I load my webpage, fill out the form and click 'Click me' : 我加载我的网页,填写表格,然后点击“点击我”:

在此处输入图片说明

Next I click ok in the alert popup and the expected result appears: 接下来,我在警报弹出窗口中单击“确定”,然后出现预期的结果:

在此处输入图片说明

I fill out the form, but when I try to click the button nothing happens! 我填写了表格,但是当我尝试单击按钮时,什么也没有发生! Not even my alert popup. 甚至没有我的警报弹出窗口。

Here are the files I am using. 这是我正在使用的文件。

My CSS, simple2.css : 我的CSS,simple2.css:

    body, html {
        margin:0;
        padding;
        height:100%
    }

    a {
        font-size:1.25em;
    }

    #content {
        padding:25px;
    }

    #fade {
        display: none;
        position:absolute;
        top: 0%;
        left: 0%;
        width: 100%;
        height: 100%;
        background-color: #ababab;
        z-index: 1001;
        -moz-opacity: 0.8;
        opacity: .70;
        filter: alpha(opacity=80);
    }

    #modal {
        display: none;
        position: absolute;
        top: 45%;
        left: 45%;
        width: 64px;
        height: 64px;
        padding:30px 15px 0px;
        border: 3px solid #ababab;
        box-shadow:1px 1px 10px #ababab;
        border-radius:20px;
        background-color: white;
        z-index: 1002;
        text-align:center;
        overflow: auto;
    }

    #results {
        font-size:1.25em;
        color:red
    }

My HTML, simple2.html : 我的HTML,simple2.html:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <link rel="stylesheet" type="text/css" href="simple2.css">
            <title>simple II</title>
    </head>
    <body>
            <div id="fade"></div>
            <div id="modal"> <img id="loader" src="images/loading.gif" /> </div>
            <div id="results"><!-- Results are displayed here -->
                    <form method="post" name="start" target="_blank">
                            <p>Enter thing1: <input type="text"     id="thing1" name="thing1" size="10" /></p>
                            <p>Enter thing2: <input type="text"     id="thing2" name="thing2" size="10" /></p>
                            <p>Enter thing3: <input type="text"     id="thing3" name="thing3" size="10" /></p>
                            <p>Check thing4: <input type="checkbox" id="thing4" name="thing4" value=1>
                            <input type="hidden" id="state" name="state" value="one" /></p>
                    </form>
                    <button id='clickme' name='clickme'>Click me</button>
                    <script src="simple2.js?0000000000015"></script>
            </div>
    </body>
    </html>

My javascript, simple2.js : 我的JavaScript simple2.js:

    function openModal() {
            document.getElementById('modal').style.display = 'block';
            document.getElementById('fade').style.display = 'block';
    }

    function closeModal() {
        document.getElementById('modal').style.display = 'none';
        document.getElementById('fade').style.display = 'none';
    }

    function loadAjax(url,data, app, epoch, state) {
            console.log ("loadAjax urls is [" + url + "] data is [" + data + "]" );
            // document.getElementById('results').innerHTML = '';
            console.log ("line 14");
            openModal();
            console.log ("line 16");
            var xhr = false;
            console.log ("line 18");
            if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
            } else {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (xhr) {
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    closeModal();
                    console.log ("line 28");
                    document.getElementById("results").innerHTML = xhr.responseText;
                }
            }
            console.log ("line 32");
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send(data);
            console.log ("line 35");
        }
    }

    document.querySelector("#results button").addEventListener("click", function(e) {
            e.preventDefault();
            var inputs = document.querySelectorAll("input");
            var params = inputs[0].name + "=" + inputs[0].value;
            for( var i = 1; i < inputs.length; i++ ) {
                    var input       = inputs[i];
                    var name        = input.name;
                    var value       = input.value;

                    params +=       "&" + name + "=" + value;
            }
            alert( params );
            loadAjax( "/cgi-bin/simple2.py", encodeURI(params));
    });

... and finally my cgi script, simple2.py : ...最后是我的cgi脚本simple2.py:

    #!/usr/bin/env python

    import cgi
    import os
    #import cgitb
    import cgitb; cgitb.enable()  # for troubleshooting
    import time
    import calendar


    def goto_state_two( a, b, c, d ):
            print """
    Thank you!<br>
    <li>thing1 is [%s]
    <li>thing2 is [%s]
    <li>thing3 is [%s]
    <li>thing4 is [%s]
    <hr>
                    <form method="post" name="start" target="_blank">
                            <p>Enter thing5: <input type="text"     id="thing5" name="thing5" size="10" /></p>
                            <p>Enter thing6: <input type="text"     id="thing6" name="thing6" size="10" /></p>
                            <p>Enter thing7: <input type="text"     id="thing7" name="thing7" size="10" /></p>
                            <p>Check thing8: <input type="checkbox" id="thing8" name="thing8" value=1>
                            <input type="hidden" id="state" name="state" value="two" /></p>
                    </form>
                    <button id='clickme' name='clickme'>Click me</button>
                    <script src="simple2.js?%d"></script>
    """ % (a,b,c,d,calendar.timegm(time.gmtime()))

    def goto_done( a, b, c, d ):
            print """
    Thank you!<br>
    <li>thing1 is [%s]
    <li>thing2 is [%s]
    <li>thing3 is [%s]
    <li>thing4 is [%s]
    <hr>
                    <form method="post" name="start" target="_blank">
                            <input type="hidden" id="state" name="state" value="done" /></p>
                    </form>
                    <button id='clickme' name='clickme'>Click me</button>
                    <script src="simple2.js?%d"></script>
    """ % (a,b,c,d,calendar.timegm(time.gmtime()))


    form = cgi.FieldStorage()
    state = form.getvalue("state")

    if state == 'one' :

            thing1  = form.getvalue("thing1", "")
            thing2  = form.getvalue("thing2", "")
            thing3  = form.getvalue("thing3", "")
            thing4  = form.getvalue("thing4", "")

            goto_state_two( thing1, thing2, thing3, thing4 )

    elif state == 'two' :

            thing5  = form.getvalue("thing5", "")
            thing6  = form.getvalue("thing6", "")
            thing7  = form.getvalue("thing7", "")
            thing8  = form.getvalue("thing8", "")

            goto_done( thing5, thing6, thing7, thing8 )

    elif state == 'done' :
            print """
    Hurray you did it!<br>
    """
    else:
            print """

    unknown state [%s]
    <hr>
    """ % ( state )
document.getElementById("results").addEventListener("click", function(e) {
        if(e.target && e.target.nodeName == "BUTTON") {
            // Move Prevent Default into condition to allow
            // other events to bubble. 
            e.preventDefault();
            // This is a Button click we have captured
            var inputs = document.querySelectorAll("input");
            var params = inputs[0].name + "=" + inputs[0].value;
            for( var i = 1; i < inputs.length; i++ ) {
                    var input       = inputs[i];
                    var name        = input.name;
                    var value       = input.value;

                    params +=       "&" + name + "=" + value;
            }
            alert( params );
            loadAjax( "/cgi-bin/simple2.py", encodeURI(params));
       }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM