Load URL image from get API

Need some coding help!

GOAL: Display images(url) pulled from DB

CURRENTLY:
I can get the image(url) from DB.
I can get image(url) to display(mobile) correctly if I manually insert the image(url).
** I can’t seem to figure out how to combine the 2 functions.

CODE 1: Load Image(url)
How would I insert image(url) from Z.jax?

const Image_PlanePico = symbol.nodes.Image_PlanePico;

var image1 = Z.ImageTexture(“Image(url) from API”).wrap(Z.Texture.Wrap.clamp);

parent.on(“pointerdown”, (e) => {
// Runs when pointerdown occurs on the parent node
// The argument e contains useful info about this event:
// https://docs.zap.works/studio/scripting/reference/object/events/pointerdown/
Image_PlanePico.skin (image1);
});


CODE 2: Get image(url) from DB
What code is needed get the data received from Z.jax to work with the CODE 1?

const Image_PlanePico = symbol.nodes.Image_PlanePico;

var imageData1;

// This will get the data from your DB field .image will display first one.
// Parameters to configure the AJAX request

const ajaxParameters : Z.Ajax.Parameters = {
url: “https://hook.integromat.com/amwlr9l4yfvuv44snlpuuxp9l1th5hhf”,
};

// Perform the AJAX request

Z.ajax(ajaxParameters, (statusCode, data, request) => {
if (statusCode === 0) {
Image_PlanePico.skin (“Unable to connect - check network connection.”);
return;
}
if (statusCode < 200 || statusCode >= 300) {
Image_PlanePico.skin ("HTTP request failed: " + statusCode);
return;

}

// Attempt to parse the data returned from the AJAX request as JSON
let imageData1;
try {
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
imageData1 = JSON.parse(data);
} catch (e) {
Image_PlanePico.skin("Unable to parse JSON: " + e);
return;
}

// Check the data is as expected and display it
if (imageData1.company) {
Image_PlanePico.skin(imageData1.image);
} else {
Image_PlanePico.skin (“Unexpected response from API”);
}

});

any thoughts? @stevesanerd or @George

1 Like

You are close. Some editing and tweaks.
Added console.log for errors. You can’t skin notes.

Enjoy :slight_smile:

Load URL image from get API.zpp (4.1 KB)

Steve

1 Like

Thanks, I really appreciate your help @stevesanerd

I had to tweak it a little more to get it to work via mobile.

if (imageData1.company) {
Image_PlanePico.skin(Z.ImageTexture(imageData1.image).wrap(Z.Texture.Wrap.clamp));

Additional question
Is it possible get API data when app is launched, then use that data later without having to do another API call?

Yes, just add more data and save the pull to a Val.
Ex: imageData1.image and imageData1.image2 and so on.

Steve

Great, thanks Steve!

If at all possible, do you happen to have an example of saving the pull to a Val as well as recalling the saved Val? Your help is VERY appreciated!

Try this it should work. You may need to update for WebAR. You will have to update your DB info. see note in the show script.

I store the images in Val’s that have export so you can pull them from other scripts. When you press the button it will get the saved data add it to an array and change to the next image. But don’t try to change the image till it loads the first time.

Load URL image from get API Ver2.zpp (4.5 KB)

Steve

Steve, thank you SO much for helping!

I changed the DB info, but wasn’t able to it to work correctly. I’ll play around with the code it to see if I can figure it out.

Sorry that was my bad. I sent the data to the val’s not the array. Here’s a working one.

Steve
Load URL image from get API Ver2FIX.zpp (4.5 KB)

2 Likes

That worked!!! Thanks again Steve!!! I really appreciate your expertise and willingness to take the time out of your day to help.

2 Likes

Your welcome!!
It’s my pleasure to help :smile:

Steve