Ajax success function with python: odd behavior

Multi tool use
Multi tool use


Ajax success function with python: odd behavior



I found an odd behavior when trying to use data on the browser-side, which is sent by a python script client-side.



The ajax call is:


$.ajax({
async: true,
type: "POST",
url: "/path/cgi-bin/pyscript.py",
data: JSON.stringify(my_array),
dataType: "html",
success : function(data) {
document.getElementById("ajaxAnchor").innerHTML = data + "Some random string to test display"
}
});



}



I wanted to send back to data the result of a print() in the python script, but this only works in an unusually specific case. For the printed data to be sent back, there must first exist a print() executed with an n at the end.


data


print()


print()


n


print("This line won't be sent to browser but is necessary n")
print("This text will be sent back")



And it's not just about adding the line break, because the following example won't work:


print("n This text will not be sent back")



There must be a "wasted" line for the following ones to be sent succesfully:


print("This line won't be sent to browser but is necessary")
print("n This text will be sent back")



I'm a beginner in using Ajax, so if anyone would care to explain why this happens, I'd like to understand the underlying logic. Thanks!




1 Answer
1



You are using the CGI-Protokoll. You have to first send an HTTP-Header, which is terminated by a empty line. An minimal answer would be:


print("200 OKn")
print("Your Text.")





Thanks for this input. Looking into the CGI protocol, I found some explanation on why the first line is necessary here, although interestingly, it doesn't matter whether or not the line actually contains a useful HTTP-header specifying the content type (e.g. Content-type: text/htmlrnrn).
– sc28
Mar 2 '17 at 13:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

S0WjrUGe Roaas
Otoe,1m7 6,wfkxuBDB2Cr49xBdpx7v,3JFyK r0 Hh4kComt,bjvf rAg8BZHkgZI8,IT gGm9,NFYhtEKP 4h

Popular posts from this blog

Rothschild family

Cinema of Italy