0

I understand this question has probably been asked multiple times but I am struggling to understand how javascript handles these types of requests.

Re the below code - I know it works as I've tested and my database updates correctly.

function post_ajax(url, token, data) {

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            console.log(this.readyState)
            //console.log("ALL OK")
            //console.log(this.responseText)
            //return this.responseText
        } else {
            console.log(this.readyState)
            //console.log(this.status)
            //console.log(this.statusText)
            //return this.responseText
        }
    };
    console.log("open")
    xhttp.open("POST", url, true)
    console.log("set request")

    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    xhttp.setRequestHeader("Authorization", "Bearer " + token)
    console.log("send")
    xhttp.send("reply_json=" + JSON.stringify(data))
    console.log("done")

}

I have a few questions.

1) the console shows this output and it looks likes its run through multiple times when I only need it to run once. Is this output correct? Is it actually running multiple times?

open
functions.js:19 1
functions.js:27 set request
functions.js:31 send
functions.js:33 done
functions.js:19 2
functions.js:19 3
functions.js:14 4
functions.js:25 open
functions.js:19 1
functions.js:27 set request
functions.js:31 send
functions.js:33 done
functions.js:19 2
functions.js:19 3
functions.js:14 4
functions.js:25 open
functions.js:19 1
functions.js:27 set request
functions.js:31 send
functions.js:33 done
functions.js:19 2
functions.js:19 3
functions.js:14 4
functions.js:25 open
functions.js:19 1
functions.js:27 set request
functions.js:31 send
functions.js:33 done
functions.js:19 2
functions.js:19 3
functions.js:14 4
functions.js:25 open
functions.js:19 1
functions.js:27 set request
functions.js:31 send
functions.js:33 done
functions.js:19 2
functions.js:19 3
functions.js:19 4

2) How do I get post_ajax to return this.responseText when this.status == 200 and this.readystate ==4?

EDIT: the call to post_ajax is below - I change a "div / span" element to a "input" element so the user can change their post.

I'm storing information I need in the ID tag of the element and extracting that into JSON to be passed to the server long with the text from the field.

I then am going to update the database and change it back to the "div" once confirmed by the server the request has been accepted.

function change_reply_to_text(ele_name) {
    // split id of the edit button to get the reply_id - post_id
    const split = ele_name.split("-")
    // get element
    var txt_reply_element = document.getElementById(split[0] + "-" + split[1] + "-" + split[2])
    // store text
    const txt_value = txt_reply_element.value

    // get button element and retrieve token for api call
    var btn_ele = document.getElementById(split[0] + "-" + split[1] + "-" + split[2] + "-edit")
    const token = btn_ele.getAttribute("token")

    // test post the ajax form
    var response = post_ajax("/api/post/update", token, get_json_from_id(btn_ele.id, txt_value))

    // create new item replacing textbox with P element
    var reply_text_p = document.createElement("p")
    // set text and formatting
    reply_text_p.innerText = txt_value
    reply_text_p.id = txt_reply_element.id
    reply_text_p.className = "article-content ow-break-word"
    // replace both
    txt_reply_element.parentNode.replaceChild(reply_text_p, txt_reply_element)

    // change button from "Edit" to "Save"
    var new_btn = document.getElementById(split[0] + "-" + split[1] + "-" + split[2] + "-" + "edit")
    new_btn.innerText = "Edit"

    // change on click back so element can now be editied again
    new_btn.onclick = function () {
        change_text_to_reply(this.id)
        //update_reply(this)
    }
}

At this point I want to have access to the response so i can determine whether Its ok to proceed or not.

// test post the ajax form
var response = post_ajax("/api/post/update", token, get_json_from_id(btn_ele.id, txt_value))
Lewis Morris
  • 1,916
  • 2
  • 28
  • 39
  • Please show the code that calls post_ajax – mplungjan May 29 '20 at 08:46
  • @mplungjan Thanks I've updated my post. – Lewis Morris May 29 '20 at 08:49
  • 1
    AND where do you call change_reply_to_text? – mplungjan May 29 '20 at 08:53
  • 1
    1. `change_reply_to_text` must be being called multiple times. 2. [Yet another duplicate of this](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Quentin May 29 '20 at 08:54
  • on a "button" onclick event – Lewis Morris May 29 '20 at 08:55
  • @Quentin THANK YOU. I've now understood something about javascript I did not know when I woke up today. I've managed to sort it out. It runs once and I can get the response. I've made the post_ajax the onclick even and pass in the change_reply_to_text as a function that I call on "success:change_reply_to_text". – Lewis Morris May 29 '20 at 09:26

0 Answers0