Jaggeryjs : Ajax call GET method

267 Views Asked by At

Working on WSO2 UES application for a dashboard project, in this, need to call REST api using GET method with HEADER data. (var headers ={"Authorization",getSecureToken};)

http://jaggeryjs.org/documentation.jag?api=get not giving enough info

<%      
    var getSecureToken = session.get("wso2-token");     
    var headers ={"Authorization",getSecureToken};      
    var userListUrl = "https://00.000.00.00:09000/users/1.0.0/users/list";      
    var userListData = get(userListUrl,headers,"json");     
    log.info(userListData);
%>

Any help will be appreciated. :-)

Update: GET method function :

function FnMakeRequestGETCall(URL, METHOD, BASICAUTH, CONTENTTYPE, ACCEPTTYPE, INPUTDATA){
        var VarBasicAuthCode = session.get('wso2-token');
        xhr = new XMLHttpRequest(); 
        xhr.open(METHOD, URL);  
        xhr.setRequestHeader("Authorization" , VarBasicAuthCode);   
        xhr.setRequestHeader("Content-Type", CONTENTTYPE);
        xhr.setRequestHeader("Accept", ACCEPTTYPE);
        xhr.send();
        var VarResponse = xhr.responseText;
        return VarResponse;
    }

var userListUrl = "https://99.999.99.999:9445/users/1.0.0/users/list";
var headers ={"Authorization",getSecureToken};

var usersList = FnMakeRequestGETCall(userListUrl,"GET","Basic RlN4S2RrZEpNN3VaYWhHN0NFcEtlaTZEa3RzYTpXbmUxd29seHp2UTNSQ2RZbXhUUTJ2WkJTd0Fh","application/x-www-form-urlencoded; charset=UTF-8","application/json; charset=utf-8","");   

 log.info(usersList);
1

There are 1 best solutions below

0
Nipuna Marcus On

I think you can use the get() instead of creating a new XMLHttpRequest(). I think the problem of your get() is that it is asynchronous. So the usrsList variable doesn't get any value assigned right away. So what you basically have to do is use the get() and pass a success function.

General get() usage is as below.

get(url[, data][, headers][, data-type][, success(data, xhr)]) 

In your case get() should look as below.

get(userListUrl,null,headers,'text',function(usersList,xhr){
   log.info(usersList);
});

If you used the XMLHttpRequest() because of the multiple header adding ability you can do the same with get. what you have to do is add those to your headers object.

var headers ={
  "Authorization":VarBasicAuthCode,
  "Content-Type" : CONTENTTYPE,
  "Accept" : ACCEPTTYPE
}; 

please buzz me if this didn't work out.

(Note: I think you added your IP address with the question update ... :D)