How to call function from URL in Javascript?
Create the Function
First of all, you start out by simply writing a normal Script Function, like this:Function HelloEcho(String $msg) : String Begin
Return $msg;
End
Set the Access Policy
Next, you'll need to set the proper Access Policy for your Script Function. This is something you should give a bit of thought to, because it's a potential security risk.For example, if you create a Script Function that returns a user's salary based on the user ID, making the Script Function public would mean that anyone would be able to read everybody else's salary, which is very likely not what you want.
Important! Using Access Policies only applies to Script Functions located in the Base Package. If the Script Function is in a different Package than Base, define an Entry Point instead.
Call the Function
To call the Script Function from the browser, simply use this URL in your browser:/script/HelloEcho?msg=Hello
Check your Parameters
If the Script Function has additional parameters that you did not specify, their value will be null. In other words, if you want a parameter to be null, do not write something like this:/script/HelloEcho?msg=null
This is bad! It will set the parameter msg to the String null instead of to the null value.
In addition to Strings, you can also send ints, float values, and Boolean values.
Headers
In our example, we also had to set some header values. In the Script Function that is being called using a URL, you can access the request and response objects using the functions HTTPREQUEST() and HTTPRESPONSE().For example, if your function returned a JPEG image instead of text, you would set the proper content type like this:
Function GetImage(String $imageId) : String Begin
If HTTPRESPONSE() != null Then
HTTPRESPONSE().setContentType('image/jpg');
End
/