0

I have javascript HTML page which has an action POST to a python file. The python code is supposed to send back a JSON to the HTML. I just do not know how to catch the JSON and parse it. I think I can parse it fine if I catch it, and for the life of me I can't get this to work. Below is the HTML code excerpt as well as the python code excerpt. The JSON is even supposed to be really simple with just one value in it!

HTML CODE EXCERPTS:

<form action="http://www.MYURLHIDDENHERE.com/cgi-bin/new.py" id="form" method="post" enctype="multipart/form-data" target="resIframe">

    <div style=" width: 0px; height: 0px; overflow: hidden;" >
        <input type="hidden" name="random" id="random" value="">
        <iframe src="" name="resIframe" id="resIframe" onload="iframeLoaded();" ></iframe>
    </div>

And here's where I try to catch it:

function iframeLoaded() {
var result = $.parseJSON($("#resIframe").contents().find("html").html())
.....
}

I wonder if I am doing something wrong with the "target" stuff above? If I print $("#restIframe") its just an [object Object]. Essentially I am not sure if I am catching the JSON response back in HTML.

And here's the py code excerpt:

#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import cgi, os
import cgitb; cgitb.enable()

form = cgi.FieldStorage()
data = {'score': 50}
print "Content-Type: text/html"
#print "Content-Type: application/json"
print

print json.dumps(data)

I've spent HOURS on this and read numerous examples/blogs so any help would be really appreciated!

23
  • What does $("#resIframe").contents().find("html").html() return? It's probably better to handle the form submit via $.ajax. Without seeing the actual data returned from POST it's hard to know. Can you use your console and post the POST data return from PY? Commented Jul 11, 2015 at 0:53
  • The ("#resIframe") is only a specific Iframe where I want to print the output. Instead of $.parseJSON I have also tried $jQuery.parseJSON and none of them seem to work. I don't think POST is actually returning anything to the HTML file - that's the problem! Commented Jul 11, 2015 at 1:17
  • How do I check what the POST data return from PY is, in the console? Commented Jul 11, 2015 at 1:19
  • Chrome: stackoverflow.com/questions/9163251/chrome-source-of-post-data Firefox: addons.mozilla.org/en-us/firefox/addon/firebug Commented Jul 11, 2015 at 1:20
  • Thanks for chrome developer tools! Seems super useful for future as well - used it to check what's returned: Its just returning "null". That seems to be the problem. Looks like I am just not catching the JSON that's supposed to be sent back by the PY code. Commented Jul 11, 2015 at 1:37

1 Answer 1

0

$("#resIframe").contents().text() worked as initialxy suggested and thanks a lot to initialxy for help with debugging overall!

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

Comments

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.