1

I want to pass a JSON object of the form {key : value} having many elements to python script . I am able to send the JSON object via Ajax as i can see that in success in response variable in Ajax function.

I am using cgi.FieldStorage() to get POST data . I want to traverse the received JSON object elements and store the key and value in variables . Later , i want to use those variables in printing HTML through that Python Script . I don't know how to proceed and iterate .

1 Answer 1

1

You don't need a cgi.FieldStorage, just simply read stdin:

import sys
import json

data = json.load(sys.stdin)

on the client side, you also have to convert your data manually into a json-string:

$.ajax({
    url: "/city/cgi-bin/test1.py",
    type: "post",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({'lat':30.5 , 'lon' : 4.5}),
    success: function(response){
        //window.location="http://mycity.parseapp.com/city/cgi-bin/test1.py"
        console.log(response.message);
        console.log(response.keys);
        console.log(response.data);

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

7 Comments

i am passing data from Javascript to Python script using Ajax and Jquery . Doesn't it require cgi.FieldStorage() to get POST data in Python script ?
stdin is the way POST data gets into a cgi-script. cgi.FieldStorage only also reads from stdin. Try it ;-)
its showing internal server error . i am using ur code in python script . Also i want to print the content of 'data' in form of key,value pair in HTML . How can i do it ? print "<h1>" , json.dumps(data , indent= 2) , "<br>"
what is this internal server error; what is in the error log? are cgi-scripts working in general?
Here is my python script pastebin.com/vZY32Cpg and here is my ajax code using jquery pastebin.com/UAi0DNYD These both are working . Now i want to store the key,value pair in python script and output them using HTML from the same python script . I am not able to traverse the JSON object and store them in python script .
|

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.