6

I am making a get request form Javascript to Python. And I am trying to pass a 2D array so something like

[["one", "two"],["foo", "bar"]]

And here is what I am currently trying but it is not working.

So in my javascript I have an array that look similar to the one above, and then pass it like this

var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://192.67.64.41/cgi-bin/hi.py?array=" + myArray, false );
xmlHttp.send( null );

And then in python I get it like this

import cgi
form = cgi.FieldStorage()
array = form.getvalue("array")

But it doesn't come out right, in python then if I were to do

print array[0]
#I get -> "o"
print array[1]
#I get -> "n"
print array[2]
#I get -> "e"

and so on, but if I what I want is

print array[0]
#output -> ["one", "two"]

How can I accomplish this?

Thanks

spen123
  • 3,464
  • 11
  • 39
  • 52
  • Can you change how you receive your data in python? It would be better to change your request to read data from body. – Marko Gresak Aug 11 '15 at 00:03
  • @MarkoGrešak yes i could change it I'm just not sure what to change it to? but if you could tell me a better way to send the data fro javascript to pythonn that would be great – spen123 Aug 11 '15 at 00:08
  • [This answer](http://stackoverflow.com/questions/464040/how-are-post-and-get-variables-handled-in-python) could be a start, you send a POST request and put your data as javascript request body. It will be much easier for you if you use a library like [jQuery](http://jquery.com/), where XMLHttpRequests are a breeze with `$.ajax`. And for backend framework, pick one of [web frameworks](https://wiki.python.org/moin/WebFrameworks) listed on python webpage. You will need those for better productivity at any serious work. – Marko Gresak Aug 11 '15 at 00:13
  • @MarkoGrešak yes okay but still how to I pass a 2D array? – spen123 Aug 11 '15 at 00:17
  • You will probably have to turn it into json with `JSON.stringify` before sending and then parse it back in your python code. But I believe jQuery's `$.ajax({url: 'http://192.67.64.41/cgi-bin/hi.py', data: myArray })` will do this automatically. – Marko Gresak Aug 11 '15 at 00:22
  • @MarkoGrešak okay but how do I make the get request or does this `$.ajax({url: 'http://192.67.64.41/cgi-bin/hi.py', data: myArray })` do it? – spen123 Aug 11 '15 at 00:24
  • I said you will have to change it to POST, and you would also have to add `method: POST` to your ajax request object. GET requests can't include a body. – Marko Gresak Aug 11 '15 at 00:30

1 Answers1

5

You can't simply pass an array as a query parameter. You will need to iterate over the array and add it to the URL string such as ?array[]=one&array[]=two

const a = ['one', 'two']
let url = 'www.google.com'

for (let i = 0; i < a.length; i++) {
    // Check to see if the URL has a query string already
    if (url.indexOf('?') === -1) {
        url = `${url}?array[]=${a[i]}`
    } else {
        url = `${url}&array[]=${a[i]}`
    }
}

console.log(url)
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • so I have to pass each string separately? Also like `?array[]=one&array[]=two&array[]=foo&array[]=bar`? But then will in be in an array in python? – spen123 Aug 10 '15 at 23:49
  • You will be able to access the GET values by their key, so `array[0]` would equal `one` – Cjmarkham Aug 10 '15 at 23:51
  • okay and just to clarify `array[2]` would be foo? Is there any way to have it be a 2D array? – spen123 Aug 10 '15 at 23:52
  • You could do so by simply building the query string so the query string would read `array[][]=bar`. Which would return as `array[0][0]` – Cjmarkham Aug 10 '15 at 23:55
  • @carl-markham : Do you remember that query strings aren't infinite? What you propose will work, but, hm, I'd rather aproach the problem head on than cowing down the array. – Dalen Aug 11 '15 at 00:12