0

I am following this tutorial learning how to talk to python using the javascript. I think I did the exact same thing as the tutorial said, however, I first got the error of

Unable to create process using 'flask/bin/python json_io.py'

Then I deleted #!flask/bin/python and restart the server, and got the following error.

File "json_io.py", line 22, in worker 
for item in data:
TypeError: 'NoneType' object is not iterable

My json_io.py code is as following

import sys

from flask import Flask, render_template, request, redirect, Response
import random, json

app = Flask(__name__)

@app.route('/')
def output():
    # serve index template
    return render_template('index.html', name='Joe')

@app.route('/receiver', methods = ['POST'])
def worker():
    # read json + reply
    data = request.get_json()

    result = ''


    for item in data:
            # loop over every row
            make = str(item['make'])
            if(make == 'Porsche'):
                result += make + ' -- That is a good manufacturer\n'
            else:
                result += make + ' -- That is only an average manufacturer\n'


    return result

if __name__ == '__main__':
    # run!
    app.run()

And the index.html is as follows,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
// setup some JSON to use
var cars = [
    { "make":"Porsche", "model":"911S" },
    { "make":"Mercedes-Benz", "model":"220SE" },
    { "make":"Jaguar","model": "Mark VII" }
];

window.onload = function() {
    // setup the button click
    document.getElementById("theButton").onclick = function() {
        doWork()
    };
}

function doWork() {
    // ajax the JSON to the server
    $.post("receiver", JSON.stringify(cars), function(){});

    // stop link reloading the page
 event.preventDefault();
}
</script>
This will send data using AJAX to Python:<br /><br />
<a href="" id="theButton">Click Me</a>

Thank you.

I wanted to create a jsFiddle or something. But I cannot find such a thing for python flask and website.

WCMC
  • 1,602
  • 3
  • 20
  • 32
  • I think it is because my `data` is None. Somehow the `cars` is not captured by the python code. – WCMC Jun 06 '19 at 16:21
  • can you run `print(data)` it seems the data var is empty. I am not a jquery pro but it seems you have to add `'json'` to you ajax post call. https://www.abeautifulsite.net/postjson-for-jquery – ghovat Jun 06 '19 at 16:30
  • hi @ghovat, the result is `None`. Either `JSON.stringify(cars)` or `cars` gives None. – WCMC Jun 06 '19 at 16:34
  • 1
    the call is not seen as `json` change it to the following `$.post(url: "/receiver", data: JSON.stringify(cars), contentType: "application/json; charset=utf-8", dataType: "json", function(){});` This shoud work – ghovat Jun 06 '19 at 16:45
  • 1
    check this for reference how to post data with containing json https://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – ghovat Jun 06 '19 at 16:45

0 Answers0