0

I'm trying to get data from apex which is in this form Map<String1, List<Map<String2,String3>>> to use it in javascript before returning a result to the apex controller..

I have tried to do it this way but my webpage keep refreshing and the content of my variable is 'false' ( I have figured this out by using console.log(myVariable); )

 <apex:actionFunction action="{!myApexMethod}" name="getResult"/>

 <script>
   var res = getResult();
   console.log(res);
 </script>

Controller code..

public String jsonResult{get; set;} 
public Map<String,List<Map<String,String>>> myData  = getResult(); 

public void getResult(){
        jsonResult = JSON.serialize(myData);
    }

It's just a little part the getResult() method is so long to write here.. but you can see what type of object it returns..

1

After storing the results of the myApexMethod in a property in your controller this will rerender the script block and get the updated value This should work:

<apex:actionFunction action="{!myApexMethod}" name="getResult" rerender="script_block"/>

<apex:outPutPanel layout="block" id="script_block">
 <script>
   var res = "{!controller_property}";
   console.log(res);
 </script>
</apex:outPutPanel>

You would be better served using Visualforce Remoteing for these types of things though.

4
  • 1
    If you're going to use the method here from Eric, make sure that you escape your string for JS using '{!JSENCODE(controller_property}'. Otherwise you'll get tripped up by Irish names and so on (O'Hara etc). – Aidan Nov 12 '15 at 14:16
  • I have tried that but still I don't have the right content.. the result is null – Joey Pablo Nov 12 '15 at 14:41
  • @JoeyPablo - you will have to post the relevant part of your controller code in order to assist any more – Eric Nov 12 '15 at 14:42
  • @Eric okay i'll edit my question right now to add some of my controller code – Joey Pablo Nov 12 '15 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.