Flask 400请求不好,版本不好,语法错误

时间:2017-06-08 20:32:46

标签: python ajax forms post flask

我有一个问题,当我尝试POST表单数据时,烧瓶会给我400个错误的请求类型,错误的请求语法,错误的请求版本

@app.route('/scrape', methods=['GET', 'POST'])
def scrape():
if request.method == 'POST': # ajax method
    print "i reached here"
    scrape_params = {
        "market_name": request.form.get('market'),
        "market_id": request.form.get('market-id'),
        "rescrape": request.form.get('rescrape'),
        "update": request.form.get('update'),
        "with_investments": request.form.get('with_investments')
    }
    angellist.scrape(scrape_params, socketio);
    return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
else:
    return render_template('scrape.html')

相应的html表单是

`

<form id="scrape_form" name="scrape" action="scrape" method="post" accept-charset="utf-8">
                <ul>
                    <li>
                        <label for="market">Choose Market</label>
                        <input id="input_market" type="text" name="market" placeholder="Market" required>
                        <input type="hidden" id="market-id" name="market-id"/>
                    </li>
                    <li>
                        <span title="Rescrapes all investor information" class="checkbox-label">
                        <input type="checkbox" id="rescrape-checkbox" name="rescrape" value="true">Rescrape</span>
                        <span title="Rescrapes all companies" class="checkbox-label">
                        <input type="checkbox" id="update-checkbox" name="update" value="true">Update</span>
                        <span title="Scrapes all investments information for investors" class="checkbox-label">
                        <input type="checkbox" id="investments-checkbox" name="with_investments" value="true">Get Investments</span>
                    <li style="text-align: center;">
                        <input type="submit" value="Go" disabled="disabled">
                    </li>
                </ul>
            </form>

`

js文件中的ajax部分

`

 $("#scrape_form").submit(function (ev) {
    var frm = $(this);
    ev.preventDefault();
$.ajax({
        type: 'POST',
        url: "/scrape",
        data: frm.serialize(),
        success: function (data) {
          displayLoadingScreen();
          console.log("Scraping should have begun listen for socket events");
          updateTimeout(socket);
        },
        fail: function() {
          socket.disconnect();
        }
      });

`

我得到的控制台输出是

`

127.0.0.1 - - [09/Jun/2017 00:52:37] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00\xae\x01\x00\x00\xaa\x03\x03;/\x01\xc2\x95')
127.0.0.1 - - [09/Jun/2017 00:52:37] " «  ¬;/┬ò╠8R⌐╙ô|┬£á∩üR'▀s╝èΘ┬ô{  ::└+└/└,└0╠⌐╠¿└└ £ ¥ / 5 " 400 -
(14904) accepted ('127.0.0.1', 62524)
127.0.0.1 - - [09/Jun/2017 00:52:38] code 400, message Bad request syntax ('\x16\x03\x01\x00\xae\x01\x00\x00\xaa\x03\x03g\xb0\xc3\xce\x14\xbb\x12\xa4\xbd\x8c8\xe2\xb0eP\x1bs6\xb4m\x16\xda=\xb6(\x95\xb0e;\xf3_\xe2\x00\x00\x1cjj\xc0+\xc0/\xc0,\xc00\xcc\xa9\xcc\xa8\xc0\x13\xc0\x14\x00\x9c\x00\x9d\x00/\x005\x00')
127.0.0.1 - - [09/Jun/2017 00:52:38] " «  ¬g░├╬╗ñ╜î8Γ░eP6┤m┌=╢(ò░e;≤_Γ  jj└+└/└,└0╠⌐╠¿└└ £ ¥ / 5 " 400 -
(14904) accepted ('127.0.0.1', 62526)
127.0.0.1 - - [09/Jun/2017 00:52:40] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00\xae\x01\x00\x00\xaa\x03\x03\xdfL\xb4\xf5t$\xadM\x1f*\xa3\xf2\xe2\x8b,\xbf"\x1a.\x85>\xcf\xc5q')
127.0.0.1 - - [09/Jun/2017 00:52:40] " «  ¬▀L┤⌡t$¡M*ú≥Γï,┐".à>╧┼qj├UQ╧üO  ┌┌└+└/└,└0╠⌐╠¿└└ £ ¥ / 5 " 400 -
(14904) accepted ('127.0.0.1', 62527)
127.0.0.1 - - [09/Jun/2017 00:52:45] code 400, message Bad request version ('\xaf)\xd6\xc9\x8a\xbd\x0e\xc2vI\x05\x86\x12\xc1\x0f>\x00\x00\x1czz\xc0+\xc0/\xc0,\xc00\xcc\xa9\xcc\xa8\xc0\x13\xc0\x14\x00\x9c\x00\x9d\x00/\x005\x00')
127.0.0.1 - - [09/Jun/2017 00:52:45] " «  ¬}T|⌠╔ëaá{jàç┘┘»)╓╔è╜┬vIå┴>  zz└+└/└,└0╠⌐╠¿└└ £ ¥ / 5 " 400 -

`

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用https?因此,无需加载http://127.0.0.1:5000/scrape,而是在浏览器中加载https://127.0.0.1:5000/scrape(取决于端口)。我有一个类似的错误输出,这对我来说是有用的:)