烧瓶“ TypeError:'NoneType'对象不可迭代”

时间:2019-06-06 16:19:39

标签: python web flask

我正在关注this tutorial,以学习如何使用javascript与python对话。我认为我做的操作与本教程完全相同,但是,我首先遇到

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

然后我删除了#!flask/bin/python并重新启动服务器,并出现以下错误。

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

我的json_io.py代码如下

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()

然后index.html如下,

<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>

谢谢。

我想创建一个jsFiddle之类的东西。但是我找不到适用于python flask和网站的东西。

0 个答案:

没有答案