3

I'm trying to pass a string from Python to Javascript via ajax POST request but i'm finding serious difficulties.

I've tried both with and without using JSON.

Here's the code

JAVASCRIPT

$.ajax({
    url: url, #url of the python server and file
    type: "POST",
    data: {'data1': "hey"},
    success: function (response) {
        console.log(" response ----> "+JSON.parse(response));
        console.log(" response no JSON ---> " +response);
    },
    error: function (xhr, errmsg, err) {
        console.log("errmsg");
    }
});

Python

import json
print "Access-Control-Allow-Origin: *";
if form.getvalue("data1") == "hey":
      out = {'key': 'value', 'key2': 4}
      print json.dumps(out)

Result is a empty JSON. when i do something like JSON.parse in javascript I get a unexpected end of input error, and when i try to get the length of the response data the size I get is 0. I suppose that there should be some problems with the client server communication (I use a CGIHTTPServer) or maybe something wrong with the datatype that python or javascript expects.

I also tried without JSON, with something like Python

 print "heyyyyy"

Javascript

 alert(response) //case of success

but I also got an empty string.

Could you please give me some advices for handling this problem ? Thanks a lot!

Mirco Lcl
  • 373
  • 7
  • 19

3 Answers3

1

You may want to compare the two snippets of code CGIHTTPRequestHandler run php or python script in python and http://uthcode.blogspot.com/2009/03/simple-cgihttpserver-and-client-in.html.

There isn't enough code to tell where your request handling code is but if it's in a class inheriting from CGIHTTPRequestHandler then you need to use self.wfile.write(json.dumps(out)), etc.

Community
  • 1
  • 1
bbill
  • 2,264
  • 1
  • 22
  • 28
1

I managed to solve the problem using the method HTTPResponse from the Django Framework.

Now it's something very similar to this

PYTHON (answering the client with a JSON)

from django.http import HttpResponse
...
data = {} 
data['key1'] = 'value1' 
data['key2'] = 'value2' 
.....
response = HttpResponse(json.dumps(data), content_type = "application/json")      
print response; 

JAVASCRIPT (Retireving and reading JSON)

success(response) 
     alert(JSON.stringify(response));

Or if I just want to send a String or an integer without JSON

PYTHON (no JSON)

response = HttpResponse("ayyyyy", content_type="text/plain")
print response

JAVASCRIPT (Retrieving String or value)

success: function (response) {
    alert(response);

This works very good, and it's very readable and simple in my opinion!

Mirco Lcl
  • 373
  • 7
  • 19
0

Instead of print json.dumps(out) you should use return json.dumps(out)

The print will only display it in python's console, just as console in javascript.

rolodex
  • 558
  • 1
  • 7
  • 19
  • `json.dumps(out)` only converts out to a string, nothing else. It does indeed seem like print is the way to go http://uthcode.blogspot.com/2009/03/simple-cgihttpserver-and-client-in.html – bbill Jul 21 '15 at 13:42
  • Sorry. Should've `return` – rolodex Jul 21 '15 at 13:43