【问题标题】:Unable to POST via JSON无法通过 JSON 发布
【发布时间】:2017-02-09 13:32:22
【问题描述】:

我正在尝试将用户文本从网页发送到我的烧瓶应用程序,以在用户文本上运行脚本,然后返回结果。我遇到的问题是文本没有出现在服务器(flask_app.py)端。这是应该发送文本的 .js (index.js):

$(document).ready(function(){
    console.log('I have loaded');

    //Grab DOM elements to use later
    analyzeTextButton = $("#analyze-button");


    analyzeTextButton.click(function() {
        // get text
        text = $("#user-text").val();
        //console.log(text); //This part works

        $.ajax({
            type: "POST",
            url: "analyze",
            dataType: "json",
            data: {
                text
            },
             success: function(results, results2, verbs) {
             text = results.text;
             console.log("Success!");
             console.log(verbs);
             }
        })      
    })

这是试图接收它的 Flask 应用程序。我尝试了几个不同的版本(来自其他 Stack Overflow 问题和各种教程),但它们都不起作用。它们被标记为 content1-5。

flask_app.py:

@app.route('/analyze', methods=['POST'])
def analyze():
    print('You made it to analyze', file=sys.stderr) #This gets printed
    content = request.get_json(silent=True)
    content2 = request.json
    content3 = request.get_json()
    content4 = request.form.get('html', '')
    content5 = request.form['contents']
    print(content, file=sys.stderr) #These all return "None"
    print(content2, file=sys.stderr) #Trying to make them return user text
    print(content3, file=sys.stderr)
    print(content4, file=sys.stderr)
    print(content5, file=sys.stderr)
    text = "The text is not being found"
    results = my_script(content) #Run a script on whichever works
    return jsonify({'results': results})

这是试图发送信息的页面(index.html):

  <div class="row">
<form role="form" method='POST' action='#'>
  <textarea class="form-control" id="user-text" name="contents" placeholder="Enter a comment"></textarea>
  <button type="button" id="analyze-button" class="btn btn-default">Not Working Button</button>
  <button type="submit" id="analyze-button2" class="btn btn-default">Working Button</button>
</form>

编辑:当我查看浏览器时,我看到 POST 似乎正在发送正确的字符串:“here+is+my+text”

【问题讨论】:

    标签: javascript jquery python json


    【解决方案1】:
    data: {
          text
    }
    

    应该是正确的 JSON,应该是

    data: {
          "value":text
    }
    

    其中value 是键,文本变量是值。

    【讨论】:

    • 我按照你的建议添加了“价值”,但我仍然得到相同的结果。
    • 您遇到的错误是什么,您的烧瓶应用程序是否能够使用value 键接收 JSON 数据请求?
    • 在浏览器中检查 POST 请求实际发送的内容。
    • 它似乎正在发送正确的值。 “值”更改后为:value:"here+is+my+text"
    • 那么问题在烧瓶方面,看看这个code.runnable.com/UpYDdiBGFJNXAAAz/…
    【解决方案2】:

    需要指定文本为html的请求:

        $.ajax({
            type: 'POST',
            url: "analyze",
            data: {html:text},
            dataType: 'json',
    
            success: function (ret) {
              alert('JSON posted: ' + JSON.stringify(ret));
            }
          });
    

    在烧瓶应用程序上,可以使用以下行读取请求:

    content4 = request.form.get('html', '')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      相关资源
      最近更新 更多