0

I'm trying to write a simple web application that uses JQuery to call a python script. The python script returns data in JSON format. Here is the ajax call in my Javascript:

console.log('before');

$.ajax({
    dataType: "json",
    url: "simple.py",
    success: function() { alert("Success"); },
    error: function(request, error) { console.log(request); }
});

console.log('after');

Here is my python script:

#!/usr/bin/env python
import json

l = "hello"
print json.dumps(l)

When I run the Javascript, I get an "Internal Server Error". The same thing happens if I change dataType to "script" in my ajax call, or change print to return.

When I instead use a plain JSON file like this one that I got from some example online:

{
  "one": "Singular sensation",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

with dataType: "json" and url: "test.json", it works fine. When I use a python script that prints out a bunch of HTML instead of JSON, that works fine too. When I run simple.py on its own from the command line, it prints out the result the way it should as if nothing's wrong. However, for some reason my JQuery just won't receive JSON data. In the Web Inspector, under "Network", the type of simple.py is listed as text/html, which seems incorrect. I think this should maybe be application/json but I'm not sure how to change it.

I'm using the Apache2 server that comes with Mac OSx, and I've put all my website files under Library/WebServer/Documents. I'm accessing my website through the "localhost" url.

Errors in log:

(8)Exec format error: exec of '/Library/WebServer/Documents/UBCNetworks/simple.py' failed, referer: localhost/UBCNetworks/index.html
 .... Premature end of script headers: simple.py, referer: localhost/UBCNetworks/index.html 
6
  • 2
    Internal Server Error means that the server script is failing, so nothing on the client is relevant. You need to check your log for the error from python.
    – Barmar
    Commented May 10, 2013 at 19:18
  • What log do you mean? I don't see any log files in the Library/WebServer directory, and nothing is getting printed to my Mac Console.
    – FrancesKR
    Commented May 10, 2013 at 20:02
  • I'm not very familiar with running webservers on Macs, so I don't know where it logs errors from the server scripts. Try searching serverfault.com.
    – Barmar
    Commented May 10, 2013 at 20:06
  • Okay, I found the log file. This is the error there: (8)Exec format error: exec of '/Library/WebServer/Documents/UBCNetworks/simple.py' failed, referer: localhost/UBCNetworks/index.html .... Premature end of script headers: simple.py, referer: localhost/UBCNetworks/index.html .... Any idea what this means?
    – FrancesKR
    Commented May 10, 2013 at 20:27
  • 1
    From what can tell by googling that error, it means you're not sending the headers properly with print "Content-type: text/html\n\n". You don't seem to be sending any headers at all, maybe that's the problem.
    – Barmar
    Commented May 10, 2013 at 20:37

1 Answer 1

2

Solved this! I had to add the following line to my python script:

print "Content-type: application/json\n\n";

This was apparently what the "Premature end of script headers" error meant... this was the script header that was missing. My javascript now prints out "Success!" as well as "hello", the JSON object returned by the python script. In the Web Inspector, the type of simple.py is now application/json instead of text/html.

(Thanks Barmar for the help!)

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.