0

The idea is I'm trying to send a set or list to the backend. I'm using JavaScript on the front end and sending the data via fetch. For the backend I'm using Flask.

const walls1 = new Set()

// Submit Button
document.addEventListener('DOMContentLoaded', function(){
    document.querySelector('#search_button').onclick = function(){
        // Send a GET request to the URL
        fetch('http://127.0.0.1:5000/search',{
            method:"POST",
            body: JSON.stringify(walls1),
            headers: {
                "Content-Type": "application/json" 
            }   
        })
        // Put response into json form
        .then(response => response.json())
        .then(data => {
            console.log(data)
            console.log(walls)
        })
    };
});

All I want to do is print this set in my backend for now, but can't seem do this successfully.

@app.route("/search", methods=["POST"])
def search():
    
  
    print(request.json) **// prints {}, but the set does have items, so this isn't right**
    print(request.data) **// b'{}'**
    info = {"success": True}
    return jsonify(info)

Update: It looks like the set I am sending from the front end to the backend is empty? But I don't understand why because console.log(walls) prints a non empty set

0

1 Answer 1

0

try convert set to array JSON.stringify(...walls1)

Sign up to request clarification or add additional context in comments.

4 Comments

That seems to do it, not sure why backend doesn't like the set. Is there a reason why you can't send a set?
this is not backend, stringify method not working with set, check this out stackoverflow.com/questions/31190885/json-stringify-a-set
why do you need set?
I only needed the set on front end, I guess once sent to the backend, it could be a list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.