简体   繁体   English

无法通过JSON发布

[英]Unable to POST via JSON

I am trying to send user text from a web page to my flask app to run a script on the user text and then return a result. 我试图将用户文本从网页发送到我的flask应用程序,以对用户文本运行脚本,然后返回结果。 The problem I am having is that the text isn't appearing on the server (flask_app.py) side. 我遇到的问题是该文本未出现在服务器(flask_app.py)一侧。 Here is the .js that is supposed to be sending the text (index.js): 这是应该发送文本的.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);
             }
        })      
    })

Here is the Flask app that is trying to receive it. 这是试图接收它的Flask应用程序。 I've tried several different versions (from other Stack Overflow questions and various tutorials) but none of them work. 我尝试了几种不同的版本(来自其他Stack Overflow问题和各种教程),但是它们都不起作用。 They are labeled content1-5. 它们被标记为content1-5。

flask_app.py: 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})

Here is the page that is trying to send the information (index.html): 这是试图发送信息的页面(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>

EDIT: When I look in my browser, I see that POST appears to be sending the correct string: "here+is+my+text" 编辑:当我在浏览器中查看时,我看到POST似乎正在发送正确的字符串:“ here + is + my + text”

data: {
      text
}

should be proper JSON,it should be something like 应该是正确的JSON,应该是类似

data: {
      "value":text
}

where value is key and text variable is value. 其中value是键,文本变量是值。

The request needed to specify that the text was html: 该请求需要指定文本为html:

    $.ajax({
        type: 'POST',
        url: "analyze",
        data: {html:text},
        dataType: 'json',

        success: function (ret) {
          alert('JSON posted: ' + JSON.stringify(ret));
        }
      });

On the flask app the request can be read with this line: 在flask应用程序上,可以通过以下行读取请求:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM