3

I'm passing data to a python script with ajax but I can't get that data back. The most I managed to get is 'undefined' or the script text itself (no idea why...). Currently, after a lot of tries, I get nothing.

I don't know if this is related but I'm new to python and didn't want to use any framework, so I'm using SimpleHTTPServer. Anyway...

This is the ajax call

$(function(){
    $.ajax({
        type: "GET",
        url: "con-test.py",
        dataType: "json",
        success: function (responseText) {
            alert(responseText); /* responseText.stuff is not working either */
        }
    });
});

And the script con-test.py

import json

result = {}
result['stuff'] = 'banana'
json_string = json.dumps(result)

print result # also tried return result
1
  • There is a sample of SimpleHTTPServer sending an ajax response in in the simplehttpserver tag Commented Jun 11, 2013 at 0:21

1 Answer 1

1

You can't use SimpleHTTPServer for web-apps. It is not executing any code, it simply serves raw files (that's why you only managed to get the script text). I suggest using some web-framework; see this question.

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

3 Comments

Oh... I see. Well can you suggest a simple framework? I tried django and it's nice but I'm looking for something less restricting (I may not want to use MVC).
@YoniLevy Django is not restricting at all. It is a set of nice modules (templating, ORM, l10n, URL dispatcher, etc.) and you are free to use only the ones you find useful for your project. I can't really suggest anything as I'm using Django, but hm… well, probably you should look at Flask.
web2py Web Framework is also lightweight. A complete list is available here

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.