XMLHttpRequest.responseText doesnt write the value when calling a URL

前端 未结 2 864
遇见更好的自我
遇见更好的自我 2021-01-22 20:36

There may be a small error in my code. please advice me.

I want to call a URL and display the value in div on pageload.I wrote this code from SO but the responseText do

2条回答
  •  太阳男子
    2021-01-22 21:14

    req.onreadystatechange = function () { document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText; }

    you have to check, if the request was successful:

    if (req.readyState === 4) { // what should be done with the result }

    finally, it has to look like this:

    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText;
    }
    }

提交回复
热议问题