Seeking Forge comp. that demod calling an API from javascript.
Question

Some weeks ago a forge component was published that showed how to call an external API via javascript (pure client side). I need this for one of our projects. Does anyone know which component this is, I can't find it anymore :(.

I have a hard time using the methods I normally use (via promises) because ES6 isn't supported and babeljs.io gives me a converted script that OutSystems doesn't like. And no, I can't use the regular integration components due to limitations on the project site (always local network but seldom an internet connection)

Greetings,

Vincent

mvp_badge
MVP
Solution

What about using XMLHttpRequest()

function callAPI() {
    var xhttp = new XMLHttpRequest();
    xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&type=track');
    xhttp.setRequestHeader('Content-Type', 'application/json');
    xhttp.setRequestHeader('Authorization', 'Bearer <MY_ACCESS_TOKEN>');
    xhttp.send();
    var response = JSON.parse(xhttp.responseText);
}
mvp_badge
MVP

Hi Vincent,

Is it not an option to use jQuery $.ajax() function?

Regards,

Daniel

Hi Daniel,

I prefer not to use JQuery at all. It doesn't play well with Reactive and it's more bloat that don't I want to have.

Greetings,

Vincent


mvp_badge
MVP
Solution

What about using XMLHttpRequest()

function callAPI() {
    var xhttp = new XMLHttpRequest();
    xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&type=track');
    xhttp.setRequestHeader('Content-Type', 'application/json');
    xhttp.setRequestHeader('Authorization', 'Bearer <MY_ACCESS_TOKEN>');
    xhttp.send();
    var response = JSON.parse(xhttp.responseText);
}

Hi Daniel,

I missed your post but just after my last post I also tried this one and it worked. Thanks for the suggestion :) 

Greetings,

Vincent 

Hi there, I'm trying to call API in client side using Javascript as shown below. I'm able to get the values in the console of the browser but unable to bring it to the  output parameter. Could someone or @Vincent Koning help me with it? Thanks!


I think one part of the issues lies in Line 12. Don't convert the reponsetext to a Json object. Do this after the javascript action with the native widgets. 

Thanks for the response. Had tried without converting to Json object but didn't work. In line 11 output parameter is text, should it be any other datatype?    

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.