Hong
July 28, 2021, 2:41pm
1
Hello,
I am using Invoke-UDJavaScript -Javascript and it is working well.
I want to use the result from javascript in Dashboard. How can I pass the value of mylist from javascript back to dashboad ?
New-UDButton -Id ‘test’ -Text ‘test’ -OnClick {
Invoke-UDJavaScript -Javascript “test();”
}
the javascript file:
function test()
{
…
var mylist = result_array
}
any help is highly appreciated.
Hong
August 2, 2021, 9:55am
2
I really need to have a solution to my prolem above.
Please kindly help.
adam
August 2, 2021, 11:10am
3
There currently isn’t a way to transfer a value from JS back to PS. You would likely need to call an API endpoint from JavaScript and then invoke some PowerShell to store that data and then read it back in the dashboard.
Hong
August 2, 2021, 12:06pm
4
Many thanks Adam ! i am actually a beginner. Would you please have a small example, just a view lines, for me? That would help me a lot.
Hong
August 3, 2021, 12:16pm
5
I would appreciate an example for how to call API (POST?) from Javascript and how to get it from a dashboard.
adam
August 3, 2021, 2:37pm
6
I’m working on an example. Should have one soon.
adam
August 3, 2021, 2:45pm
7
In your dashboard, you can setup this:
New-UDDashboard -Title "Hello, World!" -Content {
New-UDButton -Text "Send From JavaScript" -OnClick {
Invoke-UDJavaScript "fetch('/object', { method: 'POST', body: 'hello from js!'})"
}
New-UDButton -Text 'Get From Cache' -OnClick {
$Value = Get-PSUCache -Key 'FromJavaScript'
Show-UDToast $Value
}
}
In my /object endpoint, I’m using the cache.
Set-PSUCache -Key 'FromJavaScript' -Value $Body
So the I can click the first button to set the item from javascript and click the second button to read it.
1 Like
Hong
August 5, 2021, 2:48pm
9
Thank you very much Adam! It works!.
1 Like