Question: pass data from ajax success data to ejs templates

Question

pass data from ajax success data to ejs templates

Answers 1
Added at 2016-12-28 07:12
Tags
Question

I just started to use ejs. Now, I want to pass data gotten from ajax result to two different div in ejs.

I can pass to a single div but for multiple divs, what change do I need in my code?

  $.ajax({
      type: "POST",
      url: serviceurl + 'Get',
      dataType: 'json',
      crossDomain: true,
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({  "imagesrc":  imagesrc }),
      success: function (data) {
           var  property = JSON.parse(data.GetResult);
           $('.insideContent>').remove() ;                              
           $('.insideContent').addClass("insidecontentcss") ;  

           var str = $("#insidecontenttooldata").html();
           var data = { tooldata: property };
           var content = new EJS({ text: str }).render(data);
           $(".insideContent").html(content);

           var str1 = $("#well").html();
           var data1 = { tooldata: property };
           var content1 = new EJS({ text: str1 }).render(data);
           $(".well").html(content);
      }

and in ejs page i use two script like

 <script id="insidecontenttooldata" type="text/x-ejs-template">
         //// loop////
</script>
 <script id="well" type="text/x-ejs-template">
      ///loop///
</script>
Answers to

pass data from ajax success data to ejs templates

nr: #1 dodano: 2016-12-28 07:12

I guess you have error while targeting second template. Modify code as follows:

 var str1 = $("#well").html(); //id of template is 'well' and not the 'welldata'
 var data1 = { tooldata: property };
 var content1 = new EJS({ text: str1 }).render(data1);
 $(".well").html(content1);

Note: as suggested by @kristjan reinhold modified the code to use relative variables.

Source Show
◀ Wstecz