Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

python - Reciving JSON with FastAPI-backend

I have a little problem with sending JSON from my frontend to my backend and reading it there. At first the easy part, in my index.html I read what's written in some fields and pass it to var inputs

<script type="text/javascript">
    function get_inputs()
    {
        var inputs =
            {
                T_0 : document.getElementById("a").value,
                E_A : document.getElementById("b").value,
                           }
        backend_test(inputs)
        console.log("input:" + JSON.stringify(inputs))
        return inputs
    }
</script>
question from:https://stackoverflow.com/questions/65935720/reciving-json-with-fastapi-backend

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should convert values to integer.

Your model expects integers.

class Liste(BaseModel):
    T_0: int
    E_A: int

Which means you should send like this

{"T_0": int,"E_A": int}

But in your current situation, you are sending string instead of int.

Convert it to int using Number or parseInt then it should work.

Change your request method to post.

type: 'POST'

Also your change your method to post on your server side.

@app.post("/compute")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...