Forum Stats

  • 3,852,446 Users
  • 2,264,105 Discussions
  • 7,905,066 Comments

Discussions

passing parameters from javascript to sites template

3692119
3692119 Member Posts: 8
edited Jan 17, 2019 8:13AM in WebCenter Sites

Hi,

I have a requirement as below:

use javascript to access an API and receive the results in JSON format. Then, I need to pass the JSON information ( a part of it) to a sites template. Since the number of values to be passed is around 15 in number, is there a way to pass that many values to a template?

Thanks

Tagged:

Answers

  • BSR
    BSR Member Posts: 123 Blue Ribbon
    edited Oct 26, 2018 12:30PM

    Hi, I think you should be able to do it without any problem. I'm assuming you are going to send 15 GET request parameters along with Template call. Don't forget to add these 15 params to template's cache criteria if you expected a cached template.

  • Nirav Varma
    Nirav Varma Member Posts: 484 Gold Badge
    edited Oct 29, 2018 6:44AM

    Following is very basic workflow

    1. Template A contains the code for javascript which would accesses API and just prints the output of the javascript code. You can simply create SiteEntry/CSElement to write your code.

    2. Lets say you want to use the JSON object in Template B. Then you can simple use the following to call SiteEntry which you created in 1st step: ics.readPage which should provide you output as string.

    Regards

    NV

  • 3692119
    3692119 Member Posts: 8
    edited Dec 19, 2018 6:27PM

    Hi Varma,

    Can you please elaborate on point 1 above. How can i get the output from javascript to Template A.

    Thanks

    Arun

  • 3692119
    3692119 Member Posts: 8
    edited Dec 20, 2018 9:32AM

    Hi BSR,

    Thank you for your reply. But, I don't want to use GET method, as it passes all the parameters through the URL and it looks weird. So, is there a way to hide the parameters? (like vanity URL).

    We are using Sites 11.1.1.8

    Thanks

    Arun

  • 3692119
    3692119 Member Posts: 8
    edited Dec 20, 2018 9:34AM

    Thank you Varma,

    for your reply. Just wanted to add that our environment is 11.1.1.8, patch 8. We are planning to upgrade the patch level to the latest. Appreciate your support.

    Thanks

    Arun

  • Graham L
    Graham L Member Posts: 69 Blue Ribbon
    edited Dec 20, 2018 2:38PM

    Hi Arun,

    Is your API that you need to call to get the JSON in WebCenter Sites or a 3rd party service?

    Do you mean the JSON has 15 key/value pairs that you need to use or 15 different JSON strings?

    You have many different ways to solve this problem but I will elaborate once I get your reply.

    Regards,

    Graham

  • 3692119
    3692119 Member Posts: 8
    edited Jan 16, 2019 1:32PM

    Hi Graham,

    The API that I am calling is a third-party API

    JSON response has 15 key/value pairs and some are nested

    Thanks

  • Graham L
    Graham L Member Posts: 69 Blue Ribbon
    edited Jan 17, 2019 8:13AM

    I am not sure I understand.

    Your page is constructed on the server and not on the client so how can you pass in parameters from a javascript request? Once you are on the client the page has been constructed already.

    Can you give a more specific example of what you are trying to do rather than how you think you need to achieve it?

    If you need the response from your API in order to pass into your template then you will need to make the call on the server side in Java or Groovy and not in Javascript. You can pass in your json as a single parameter or broken up into individual parameters. If you use a single parameter then you have to be careful because if your JSON response has the same data but in a different order then it will generate a new cached pagelet. Anyway more information will help here.

    Regards,

    Graham