Vik Vik - 2 months ago 9
Javascript Question

callback not invoked on response

i have the ui code like

<div class="panel-heading">
Log Messages&nbsp;&nbsp;&nbsp; <button type="button" class="btn btn-primary" onclick="onrefresh()">Fetch New Messages</button>
</div>


the involved js is

var dt = $(document).ready(function() {
var hostName = 'http://127.0.0.1:7101';
...some code

});

function onrefresh(){
console.log("he viti");
xmlhttp = new XMLHttpRequest();
var url = "http://127.0.0.1:7101/LogAnalyzer-RESTWebService-context-root/rest/v1/LogMessagesVORest";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/vnd.oracle.adf.action+json");
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert('we are back');
location.reload();
}
}
xmlhttp.send(JSON.stringify(parameters));
}


The callback does not happen in the onrefresh method. However, I have noticed that methods thats inside the top part involving jquery there it works fine.

Answer

try this

request.addEventListener('load', function(event) {
   if (request.status >= 200 && request.status < 300) {
      console.log(request.responseText);
   } else {
      console.warn(request.statusText, request.responseText);
   }
});
Comments