Victor
Victor

Reputation: 152

XMLHttpRequest sends b'' instead of json data

I'm trying to send json data from my web app to my python flask api, my server is receiving the POST rqeuest & is receiving data, the issue is that it's not receiving json data rather the string b''

This is the javascript code I got to send the json data to my api

function onSubmit() {
    document.getElementById("orderbutton").disabled=true;
    setTimeout('document.getElementById("orderbutton").disabled=false;',3000);
    var request = new XMLHttpRequest();
    var url = "http://127.0.0.1:5000/api/order";
    request.open('POST', url, true)
    request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    request.onload = function() {
        var response = JSON.parse(request.responseText)
        console.log(response)
    }

    request.send(JSON.stringify({"id": document.getElementById("order").value.trim()}))
}

edit: it's not an issue with document.getElementById("order").value.trim() because when I console.log it, I get the input I put in my <input> field

Upvotes: 0

Views: 44

Answers (2)

apridos
apridos

Reputation: 182

use .decode('utf-8'), Let me know the result.

Upvotes: 0

Julius
Julius

Reputation: 501

Try editing your send() line to

request.send(JSON.stringify({id: document.getElementById("order").value.trim()}));

Change is from "id" to id

Upvotes: 1

Related Questions