POST方法在被请求时不断发送400个错误

捷豹515

我目前一直在与Flask&MongoDB合作,尝试一个小型应用程序。我目前正在让用户获得MongoDB集合中的所有法律,并通过将JSON发送到服务器,通过POST方法对MongoDB集合创建新法律。

但是,下面的输出显示将POST请求发送到/ post / law将发出400错误。

127.0.0.1 - - [12/Aug/2020 09:21:50] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2020 09:21:50] "GET /laws HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2020 09:21:54] "POST /post/law HTTP/1.1" 400 -

我一直对为什么会发生此错误感到困惑。我知道通常有400个错误与错误请求有关,但是,我不确定错误请求错误发生在哪里?

# This handles the POST request
@app.route('/post/law', methods = ["POST"])
def post_law():
    if request.is_json:
        content = request.get_json()
        print(content)

客户端:

<!--This tries to send a request to /post/law-->
<h1>Submit a Law</h1>
<form action="", method="GET">
    <div id="law_name_section">
        <label>Name of the law:</label>
        <input type="text" required id="law_name">
    </div>
    <div id="law_description_section">
        <label>Description of the law:</label>
        <input type="text" required id="law_description">
    </div>
    <div id="law_url_section">
        <label>URL to the law:</label>
        <input type="text" required id="law_url">
    </div>
    <input type="submit" value="Submit law" onclick="submitLaw()">
</form>

<script>
// submitLaw() tries to send a request to /post/law in a JSON request
    async function submitLaw() {
        let name = await document.getElementById('law_name').value 
        let description = await document.getElementById('law_description').value 
        let url = await document.getElementById('law_url').value

        let options = {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: {
                name: name,
                description: description,
                url: url,
                status: "Bill"
            }
        }

        let response = await fetch("http://127.0.01:8000/post/law", options)

        if (response.ok) {
            alert("Successfully sent data to /post/law")
        } else {
            alert(`Couldn't send data to /post/law`)
        }
    }
</script>
盖凡

可能是因为您的视图未返回响应。尝试:

@app.route('/post/law', methods = ["POST"])
def post_law():
    if request.is_json:
        content = request.get_json()
        print(content)
    return "hello"

此外,您的网址格式不正确。应该:

 await fetch("http://127.0.0.1:8000/post/law", options)

而且,为什么所有async电话呢?唯一应该异步的是您的fetch()

您也有2个submits发生。尝试这个:

<!--This tries to send a request to /post/law-->
<h1>Submit a Law</h1>
<form action="", method="POST">
    <div id="law_name_section">
        <label>Name of the law:</label>
        <input type="text" required id="law_name">
    </div>
    <div id="law_description_section">
        <label>Description of the law:</label>
        <input type="text" required id="law_description">
    </div>
    <div id="law_url_section">
        <label>URL to the law:</label>
        <input type="text" required id="law_url">
    </div>
    <input type="button" value="Submit law" onclick="submitLaw();">
</form>

<script>
// submitLaw() tries to send a request to /post/law in a JSON request
    function submitLaw() {
        let name = document.getElementById('law_name').value;
        let description = document.getElementById('law_description').value; 
        let url = document.getElementById('law_url').value;

        let options = {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: {
                name: name,
                description: description,
                url: url,
                status: "Bill"
            }
        }

        let response = await fetch("http://127.0.0.1:8000/post/law", options)

        if (response.ok) {
            alert("Successfully sent data to /post/law")
        } else {
            alert(`Couldn't send data to /post/law`)
        }
    }
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Post方法在android中返回400个错误的请求

Post 方法返回 400 个错误的请求 Angular

尝试在HTTPCLIENT -JAVA中发送POST请求时,收到400错误请求

POST方法是ajax给出400(错误请求)

WCF Post方法,返回400错误请求

$http post 方法的 400 错误请求

jQuery $ .ajax通过发送POST接收到400错误请求

使用JSON正文发送POST请求时出现400响应

收到(400)错误的请求。尝试发送GCM消息时

通过 CLI 发送 400 个错误的 Ajax 请求

添加 byte[] 数据类型时对 post 方法的 400 错误请求

使用python request.post函数时请求错误400

POST到Razor页面时出现400错误的请求

REST'put','post' 400 个错误的请求。Javascript、HTML、Java

击中发送“请求正文”的REST Controller POST方法时,获取“格式错误的语法”

发送请求到RestControler Spring MVC时,错误的请求400错误

使用php发送GET请求时出现HTTP 400错误请求错误

POST 400错误,ajax请求

python请求POST 400错误

我想使用 fetch 方法从 api url 获取请求。但我不断收到错误 400

Django发送POST请求时返回403错误

尝试发送 POST 请求时收到 404 错误

为什么在尝试发送POST请求时发生错误?

发送查询参数时POST请求发生I / O错误

使用 Node.js/got 库发送 GET 请求时出现“400 - 错误请求”

golang-tcpclient当我发送http请求时,它总是返回(错误请求400)

Python POST 请求 400 错误请求

我不断收到 HTTP 错误 400:来自 urlopen 的错误请求

Apollo客户端在向服务器发送突变时返回“ 400(错误请求)错误”