2016-09-23 22 views
-2

我有像回調不調用上響應

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

所涉及的js UI代碼是

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));  
} 

回調不會在onrefresh方法發生。但是,我已經注意到,在頂部涉及jQuery的那裏的方法,它工作正常。

+0

你的意思是'警報()'是不是之前'location.reload稱爲()'發生? – guest271314

+1

出於好奇,如果您使用jQuery,爲什麼不使用jQuery的AJAX方法? – j08691

+0

你希望我們能做些什麼?這不像我們可以測試您的服務器或查看您的開發人員工具。你做了什麼調試?參數定義在哪裏?你的服務器是否收到請求?如果是這樣,它發送了什麼?你是否在'if'之外嘗試過'alert()'? – 2016-09-23 19:25:37

回答

0

試試這個

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

我的代碼的哪一部分應該將 – Vik

+0

@Vik xmlhttp.onreadystatechange替換爲xmlhttp。 addEventListener –

+0

我得到log-messages.js:184未捕獲的ReferenceError:請求未定義 – Vik