MOCKSTACKS
EN
Questions And Answers

More Tutorials




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
The String value "Hello" will be assigned to the Script Function's parameter msg. This assignment is case sensitive. The Script Function's return value will be sent to the browser.

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
   /


Conclusion

In this page (written and validated by ) you learned about . What's Next? If you are interested in completing Javascript tutorial, we encourage you simply to start here: Javascript Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.