Accessing a 3rd API flight service not working

So I’m trying for the first time to access the skypicker api for a specific flight…
But when I wrote : " var request = new XMLHttpRequest();"
It says XMLHttpRequest is undefined…does anyone have a clue?
Do I miss something else as well?

export function searchButton_click(event, $w) {

var request = new XMLHttpRequest();

request.open(‘GET’, ‘https://api.skypicker.com/flights?v=3&sort=price&locale=en&partner=picky&flyFrom=IST&to=AYT&dateFrom=03/02/2018&dateTo=03/02/2018&typeFlight=oneway&adults=1&curr=EUR’);

request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log(‘Status:’, this.status);
console.log(‘Headers:’, this.getAllResponseHeaders());
console.log(‘Body:’, this.responseText);
}
};

request.send();
}

I also tried to write the function using WIX examples…
But still when I click my search button nothing happans on the screen…
Isn’t it supposed to show me the results in a JSON file?

fetch(‘https://api.skypicker.com/flights?v=3&sort=price&locale=en&partner=picky&flyFrom=IST&to=AYT&dateFrom=03/02/2018&dateTo=03/02/2018&typeFlight=oneway&adults=1&curr=EUR’, {method: ‘get’})
.then( (httpResponse) => {
if (httpResponse.ok)
{
return httpResponse.json();
}
else
{
return Promise.reject(“Fetch did not succeed”);

} 

} )

Hey Eilon,

You should use the second function (fetch).
As for the JSON:

return httpResponse.json();

returns a Promise - you need to set a callback for it (you’re missing another ‘then(…)’).

See full example here.

Good luck :slight_smile:

Liran.

Is it possible to use the json file and return it to a wix database?
I’m having a flights data that I get from the API,
I want to get the JSON file and insert it into the WIX database. That will help me show the JSON data easier using the database after…
Is it even possible in WIX?

Sure…
You can use ’ wixData.insert() ’ to put things into your database using code.

First, make sure to model the fields you want.
If you just want to save the whole JSON as a string, you can use JSON.strigify() .

Liran.