DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: EJS Sending Json Object to Template

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    EJS Sending Json Object to Template

    Greetings,
    I have a page that uses Jquery to get some Json encoded data from a separate script. When that data returns I want to send it to my template page and loop through the data to populate one or more forms, depending on number of records in the json data.

    Sending code in html page (in part)

    <script type="text/javascript">
    /* some code here - not needed to show */

    $("#filter-results").empty().append("back from hours-get.php");
    var result = new EJS({url: 'hours-form.ejs'}).render('data');

    /* some code here - not needed to show */
    </script>

    This code works. I'm getting data back as a Json object. My problem is I do not know how to loop through "data" on the hours-form.ejs page using the <% ... %> tags. I've tried using jquery's " $.each(data, function(i,item){ " code but I get errors.

    Would anyone know how I should send 'data' to hours-form.ejs and then loop through it?

  2. #2
    Join Date
    Oct 2008
    Posts
    2
    I gottit!!
    I downloaded jason2.js and added it to my code (included) from http://www.json.org/json2.js. To my html file I added:

    items = JSON.stringify(data);

    This takes a JSON object and converts it to an array (items).

    In my hours-form.ejs I then loop through the items array.
    Code:
    <script type="text/javascript">
     
             $(document).ready(function(){
                 $("#filter-results").html("Search Results will appear here!.");
    	         $("#job_id").bind("change", function(e){
    		        $.getJSON("hours-get.php", {job_id:$("#job_id").val()}, function(data){
                       items = JSON.stringify(data);
                       var result = new EJS({url: 'hours-form.ejs'}).render('items');
                       document.getElementById('filter-results').innerHTML = result;
                    });
    			 });   
             });
    
    </script>
    I'm good now.
    Thanks,

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX

    Thanks for posting your solution. It might help others in the same, or similiar, situation.

Similar Threads

  1. Replies: 0
    Last Post: 08-29-2002, 09:40 AM
  2. MSXML template processor error
    By John Rigsby in forum ASP.NET
    Replies: 1
    Last Post: 10-03-2001, 09:39 AM
  3. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 05:50 PM
  4. HtmlSelectElement Object
    By JD in forum Web
    Replies: 0
    Last Post: 05-01-2001, 02:46 PM
  5. Object Modeling for the WEB
    By Greg Dirst in forum authorevents.appleman
    Replies: 1
    Last Post: 04-10-2000, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center