0

I use the below code to send data (GET) from python to javascript.

in javascript:

$.get('http://localhost:5000/api/scan').success(function(res) {
        obj = JSON.parse(res);
        if(obj['channel'] == "1"){
        document.getElementById("channelZero").innerHTML = "1";
        }});

in python:

channels_str =  channels.getvalue()
data['channel'] = channels_str
return dumps(data)

how can I send data from javascript to python?

Mahsa
  • 466
  • 2
  • 7
  • 26
  • Your question is not clear. You've said "I'm sending data like this, how do I send data?" – Daniel Roseman Sep 02 '15 at 10:20
  • @DanielRoseman he's saying "here is code that works sending data from python to javascript, now I want someone to write other code to send data from javascript to python" – Jaromanda X Sep 02 '15 at 10:21
  • @DanielRoseman I want to do this vice-versa. I can send data from "python to javascript" but how is "javascript to python"? – Mahsa Sep 02 '15 at 10:22
  • see this link http://stackoverflow.com/questions/14778167/return-data-from-html-js-to-python/14778465#14778465 – Sakib Ahammed Sep 02 '15 at 10:37

1 Answers1

0

In case

$.get('http://localhost:5000/api/scan').success(function(res) {
    //your code
}});

you request scan, but if you add "?who=Masha" you send a parameter who with a value "Masha" as get request parameter.

$.get('http://localhost:5000/api/scan?who=Masha').success(function(res) {
    //your code
}});

Then if your python get response (typically def do_GET(self):) has:

data['channel'] = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('who', None)

Note that you need to import urlparse (In Python3, you'd use urllib.parse)

import urlparse

From w3schools

The GET Method

Note that the query string (name/value pairs) is sent

in the URL of a GET request:

/test/demo_form.asp?name1=value1&name2=value2 

Some other notes on GET requests:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data
Margus
  • 19,694
  • 14
  • 55
  • 103