I'm trying to write an RestClient for my own application but i could not figure out one thing.
my javascript object has the following method;
getRequest:function(path){
//Construct Url
var uri = self.baseUrl + path;
//Ajax Call : Dynamic
var xmlHttp = new XMLHttpRequest();
var apiResult;
var handleResponse = function(status, response) {
apiResult = new ApiResult(JSON.parse(response), true, status);
}
var handleError = function (status) {
apiResult = new ApiResult("", false, status);
}
var handleStateChange =function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
handleResponse(xmlHttp.status, xmlHttp.responseText);
}
else{
handleError(xmlHttp.status);
}
}
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", uri, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.setRequestHeader("X-Authorization", self.token);
xmlHttp.send();
return apiResult;
}
And i've a very simple test html for this like;
<html>
<head>
<script src="RestClient.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js"></script>
<script>
$(document).ready(function() {
var client = new RestClient("E3B55E5C-F086-4439-9268-217F9D0F5F71", "6C9FC7BD-453E-4D2A-905E-80269923D204");
client.init();
var result = client.getRequest("Api/Categories/GetAll");
if (result.StatusCode == 200){
jQuery.each(result.Data,function(index, element){
$("#categorList").append("<li>"+ element.CategoryName+ "</li>");
});
}
else{
}
console.log(result);
console.log(client.getToken());
});
</script>
</head>
<body>
<div>
<span>Kategoriler</span>
<div>
<ul id="categorList">
</ul>
</div>
</div>
</body>
If i make the call async the list does not load but if i do synchronius call its ok?
it says that the syhc calls are deprecated. So please can someone guide how to achive this async?