2016-02-05 30 views
0

在angular js中,当我们在$ http.get('')。中提供了错误的url时,我们只是得到错误消息,它不会在控制台中显示任何错误消息。如何在资源请求错误的情况下使用XMLHttpRequest函数处理程序?

例如

$http.get('path').then(onpass, onerror); 

var onpass = function(request){$scope.data = request.data }; 
var onerror = function(request){$scope.error= "error" }; 

只是想知道我们如何能够利用相同的功能的XMLHttpRequest。如果你只提供一个例子,那将是非常棒的。我用同样的

xhttp = new XMLHttpRequest(); 
xhttp.open('POST', service + url, false); 
xhttp.setRequestHeader("Accept", "application/json"); 
xhttp.setRequestHeader("Content-Type", "application/json"); 
xhttp.setRequestHeader("APIKey", apiKey); 
xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     var data = xhttp.responseText; 
     // console.log(data); 
    } 
} 
var payload = "{'xyz':{'abc':'" + test+ "', 'test':'" + test+ "'}}"; 
xhttp.send(payload); 

感谢

回答

相关问题