How to post data structure like json to flask?

后端 未结 2 1195
失恋的感觉
失恋的感觉 2021-02-13 11:43

I have a data structure like this:

\"enter

I\'m try to send it to server by $.ajax

2条回答
  •  迷失自我
    2021-02-13 12:04

    You are sending your data encoded as query string instead of JSON. Flask is capable of processing JSON encoded data, so it makes more sense to send it like that. Here's what you need to do on the client side:

    $.ajax({
        type: 'POST',
        // Provide correct Content-Type, so that Flask will know how to process it.
        contentType: 'application/json',
        // Encode your data as JSON.
        data: JSON.stringify(post_obj),
        // This is the type of data you're expecting back from the server.
        dataType: 'json',
        url: '/some/url',
        success: function (e) {
            console.log(e);
        }
    });
    

    On the server side data is accessed via request.json (already decoded):

    content = request.json['content']
    

提交回复
热议问题