How to send Json string (file) to server using JS?

  • 0
    Hello everyone.
    How can I send json string to server using XMLHttpRequest () function?

    there is such a line
    const result = "{"room":{"80-c4-48-84-79":" Federal research and production center ","73-b3-49-af-9d":" dgru","04-d2-45-86-6a":"Main","d0-51-41-bf-4f":" Publications","c5-3c-4d-83-4f":" news","7d-ae-46-a0-25":"Contacts","8f-01-4b-81-4e":"Certification center","56-32-40-82-1a":"1959","27-63-47-b9-1c":"2019","a7-34-46-b8-38":" Moscow","42-29-4c-bb-df":" text","67-8b-47-b1-34":"example"}}

    I am trying to submit using the following code, but the request does not go to the server.
    function TextJs(result) {
        var xhr = new XMLHttpRequest();
        let url = '';
        console.log(url)'GET', url, true);
        if (xhr.status != 200) {
            // обработать ошибку
            alert(xhr.status + ': ' + xhr.statusText); // пример вывода: 404: Not Found
        } else {
            // вывести результат
            names = xhr.responseText;
            let data = JSON.parse(xhr.responseText);
            massiv= data.sentences[0].trans;
        return massiv;

    Help fix the error
    JavaScript Anonymous, Jun 15, 2020

  • 1 Answers
  • 0
    I assume you need to use POST.

    And for heaven's sake, use fetch. Unless you have some concrete reasons to use XMLHttpRequest

    And in my opinion you are not performing the request correctly at all. I don't understand how you are trying to get a response without listening to onreadystatechange

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!