tags:

views:

71

answers:

5

I have a question about displaying results using ajax. I have part of the code below.

$.ajax({
    type: "POST",
    url: "Service.asmx/GetMethod",
    data: '{ "field": ' + id,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(results) {
        var html = results.d[0];
        $(contentElement).html(html);
    }
});

Need to display the data in d[0]. It consists of 3 pieces of data - product id, product name, product description. I need to figure out a way to first just display the data and then try to display using (css or html). Please help

Thanks,

SA

A: 

Is content element a div?

In any case, you might try

 $(contentElement).text(html);
Mike Blandford
+1  A: 

Here's a quick example of iterating over an array of JSON objects via jQuery, and appending data to an element on the page:

 $.ajax({
    type: "POST",
    url: "Service.asmx/GetMethod",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(results) {
        //Select the DOM element once for efficiency.
        var $contentElement = $("#contentElement");

        //Now, loop over the 'results' object returned from your AJAX call.
        $.each(results, function(i, result) {
          //result is a single item in your array of JSON objects.
          $contentElement.append('id: ' + result.id);
          $contentElement.append('name: ' + result.name);
          $contentElement.append('desc: ' + result.description);
        });
    }
});
Peter J
A: 

It did not work.

sa
You should post this in a comment, not as an answer
Justin Johnson
A: 

content element is

        var ttHtml = '<div id="tt"><table cellpadding="0" cellspacing="0"><tbody>'
                        + '<tr><td class="ttTopLeft"></td><td class="ttTop"></td><td class="ttTopRight"></td></tr>'
                        + '<tr><td class="ttLeft"></td><td id="ttContent" class="ttMiddle">'
                        + '</td><td class="ttRight"></td></tr>'
                        + '<tr><td class="ttBottomLeft"></td><td class="ttBottom"></td><td class="ttBottomRight"></td></tr>'
                        + '</tbody></table></div>';
sa
Let me ask you: which part of the process do you believe is broken?
Peter J
A: 

contentElement is dynamically created and it contains ttHtml content

sa
My code explains how to get data out of the returned JSON array. You can put the data anywhere you'd like.
Peter J