| This thread was migrated from an old forum. It may contain information that are no longer valid. For further assistance, please post a new question or open a support ticket from the Customer Support portal. |
| This thread was migrated from an old forum. It may contain information that are no longer valid. For further assistance, please post a new question or open a support ticket from the Customer Support portal. |
Hi @Alison Olivew ,
Yes you can use XMLHttpRequest to create external APIs in JS modules.
Example,
var xmlHttpRequestOptions = {};
var httpRequest = new XMLHttpRequest();
if (!params) {
params = "";
}
function errorCallback(res){};
function successCallback(res){};
var httpMethod ="POST";
httpRequest.onerror = function(res) {
errorCallback(res);
};
httpRequest.onload = function(res) {
//validations if any
successCallback(res);
};
httpRequest.ontimeout = function(res) {
};
httpRequest.withCredentials = xmlHttpRequestOptions["enableWithCredentials"] || false;
httpRequest.open(httpMethod, url, true);
Hi @Elizvxeth Poole Thanks for the response this is very useful I'll start to play on my own with modules.
@Anne Ross , @Elizvxeth Poole Hi guys I was trying to use XMLHttpRequest in Visualizer Enterprise once again the same idea to connect to an external server. This is the code:
function callApi(){
var urlApi = ‘https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=AIzaSyBVID1YuTpNxLxK0FuH1SNUwh5QIWDw2wU’;
var xhr = new XMLHttpRequest();
xhr.responseType = ‘json’;
xhr.onreadystatechange = function (){
if(xhr.readyState === XMLHttpRequest.DONE){
homescreen.mainScreen.myDashboardScreen.text = xhr.response;
}
};
xhr.open(‘GET’, urlApi);
xhr.send();
}
This time just creating a basic instance of the constructor XMLHttpRequest in visualizer, But when I run the mobile app with Visualizer Preview I get the alert in the phone:
“ReferenceError Can’t find variable: XMLHttpRequest”
Obviously this constructor is not defined normally is defined in the browsers so is easy to use. But in this case inside the phone is not defined it should be defined inside the Kony SDK isn’t it? I found something interesting but still not sure if is the right way to go: https://stackoverflow.com/questions/37819443/simple-kony-net-httprequest-not-working
It seems to exist a Kony constructor such as:
kony.net.HttpRequest();to do this kind of connection without using Kony Fabric. I’d appreciate any help with this new issue.
Kind regards.
@Alison Olivew You can make use of if condition to check for the platform and based on this you can implement your logic.
For example in attached project I checked for the platform and called the API to make the service call you shared above if the platform is Web. For native it will call httpRequest API.
kony.os.deviceInfo().name == "thinclient" is the condition used in the sample app.
@Anne Ross Thanks a lot for your response so to connect directly to an external server from Visualizer to use on the development of a mobile app, without Kony Fabric is done using the constructor kony.net.HttpRequest(); to create the API ?