1

I know this question has been asked before but I'm very confused, so sorry if the answer is already hear but I just need walking through

I want my JavaScript to my python script, pass two variables and then await a response of one variable. I know need ajax to do this. At the moment i have:

    $.ajax({
         type: "POST",
         url: "/static/test.py",           //location of python file
         dataType: 'json',
         data: {
             choice: value                 //number variable i want to pass
             equation: writteninput        // string variable i want to pass
         }
         }).done(function(ans) {
             finalanswer = ans             // ans is the variable i want to 
                                              get back and finalanswer is 
                                              what i want to use in the rest 
                                              of my js
         });

On my python side i know i need to use CGI to recieve the data however i have absolutely no clue how to do this. Here is my attempt so far:

    import cgitb; cgitb.enable()
    import cgi
    import os

    input_data  = cgi.FieldStorage()       #how do i get my two variables?

    .....python does stuff......

    print(ans)                             #ans is string

I've been stuck on this problem for a few days but due to me being new to programming I've been very confused. Any help is appreciated and thanks in advance.

Minied
  • 11
  • 3
  • See http://stackoverflow.com/questions/464040/how-are-post-and-get-variables-handled-in-python . I'm pretty sure that input_data will be a dict, so just call your keys i.e. input_data['choice'] - but you can see the key-value pairs through just printing input_data. – Laxsnor May 10 '16 at 15:07
  • I'll take a look and see if it helps, thanks :) – Minied May 10 '16 at 15:10
  • I understand how they've used the CGI but i want this take place after a button click. Any idea how i would do this? – Minied May 10 '16 at 15:15

0 Answers0