0

Edited out silly mistake

I am using AJAX to access a python script, get some TEXT from the python script & display it on my webpage.

My Problem: is that the response text is this "undefined" when it should be this "bbbb"

I am confused as to where I am going wrong? Is it my python script is incorrect (not handling AJAX (?requests?) correctly), is it my javascript or is it my WSGI server I made?

HTML & Javascript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    <!--
        function post( dest, params )
        {
            var xmlhttp;

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    return xmlhttp.responseText;
                }
            }

            xmlhttp.open("POST",dest,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send( params ); 
        }


        function onTest()
        {
            var response = post( "cgi/aaa.py", "email=blah" );
            var output   = document.getElementById( "bb" );

            output.innerHTML = response;
            alert( response );
        }
    -->
    </script>
</head>

<body>

    <p id="bb"> abcdef </p>
    <a href="javascript:onTest()">Click it</a>

</body>

</html>

My python script:

import cgitb; cgitb.enable()
import cgi
import os


input_data   = cgi.FieldStorage()

print "bbbb"
#print "you said: " + input_data['email']

3 Answers 3

1

You are getting undefined because you just did the post and didn't wait for the response to come back. Your alert call should be inside the status change, where you have return xmlhttp.responseText; right now. Remember, Javascript functions (except alert ironically) don't block.

Sign up to request clarification or add additional context in comments.

1 Comment

try to be more understanding of others. don't assume things are obvious by saying "of course..." it may be obvious to you, but it may not be to others. we all have our strengths and weaknesses, and we are all here to learn and share our knowledge.
0

Maybe you should use response, not output in this function. output references the element with id="bb", so the innerHTML is actually correctly being set:

   function onTest()
    {
        var response = post( "cgi/aaa.py", "email=blah" );
        var output   = document.getElementById( "bb" );

        output.innerHTML = response;
        alert( response );
    }

Comments

0

The problem is this:

var output   = document.getElementById( "bb" );
...
alert( output );
...
<p id="bb"> abcdef </p>

The variable output is pointing to a DOM element with id "bb", which is an HTML paragraph tag.

I think you probably wanted to print response.

2 Comments

I feel like an idiot lol, silly mistake BUT its not fixed. Now it returns "undefined"??
Yeah, your other problem is you're trying to use the output of the post function, which returns right away before the answer comes back. Anything that you do with result of the AJAX call has to happen in the callback function, in your case xmlhttp.onreadystatechange. You might want to look for simple AJAX examples so that you get the idea of how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.