My template needs to make an AJAX call to an endpoint, and depending on the data that is received, pull different values out of the JSON defined in a Function node. This data is then fed into Formio
and so, based on the results of the endpoint, the Form is built with different data.
But I can't seem to get access to msg.payload
- whenever I console log out console.log("{{{payload}}}")
I get [object Object]
as a string, but I just need it as an object.
My Function
node which defines payload:
msg.payload = {
components: [
{
label: "label one",
},
{
label: "label two",
},
],
}
return msg
my Template
node:
<!DOCTYPE html>
<html>
<body>
<main role="main" class="container">
<script type="text/javascript">
// I want to load msg.payload as an object
$.ajax({
url: "{{{base_url}}}/my_api_path_stuff",
type: "post",
contentType: "application/json",
data: JSON.stringify({}),
success: function (data) {
// depending on the results of the data, target a specific element in msg.payload's "components" key
var component = msg.payload.components[0]
if (data.required === false) {
component = msg.payload.components[1]
}
doStuff(component)
},
})
</script>
<div>my content goes here - it is not dynamic</div>
</main>
</body>
</html>
Thank you for your time and help!