8

When the time return from ajax, I should return as json encode, and use jquery.parseJSON and use document.createElement and append the data inside the Element that just created.

or it is better to return as html text?

example,

<div id="contentcontainer"></div>

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John",
   success: function(msg){
     msgObj = jquery.parseJSON(msg);
     var div = document.createElement('div');
     div.style.color="red";
     $(div).append(msgObj.name);
     $('#contentcontainer').append(div);
   }
 });

 //some.php
 if($_POST['name']){
    echo json_encode( array('name'=>$_POST['name']) );
 }

OR I should do like this?

<div id="contentcontainer"></div>

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John",
   success: function(msg){ 
     $('#contentcontainer').append(msg);
   }
 });

 //some.php
 if($_POST['name']){
    echo '<div style="color:red">'.$_POST['name'].'</div>';
 }

Ammended... sorry for my bad grammar

Of course, this is just a example, real case it would have a lot of data, may be in html table format.

Of course, this is just a example, real case it would have a lot of data.

if it has a lot of data, then I need to write a lot of document.createElement(). and it consumes time to write like this document.createElement('table'); document.createElement('tr');

instead of (Return as HTML and just append in the container)

For me I think second format( return HTML ) is easier.

But not sure for the performance wise, which is better?

Please advise.

4
  • JSON. Because it will reduce your bandwidth and separate your data from your presentation. Also, it's a bad idea to hardcode styles. Commented Nov 13, 2010 at 5:47
  • I don't think it will reduce the bandwidth. uniqsign said "Of course, this is just a example, real case it would have a lot of data, may be in html table format.", you are suggesting to encapsulate HTML to JSON which in fact will take more bandwidth. Commented Nov 13, 2010 at 5:52
  • Sorry to both of you, my mistake, I doesn't intend to mean want to encapsulate HTML to JSON, I mean if it has a lot of data, may be it is easier for me to write <table></table> instead of document.createELement. Commented Nov 13, 2010 at 7:10
  • In addition to that, if you write a lot of document.createElement,$('#xx').append(data) your code will be longer and hard to read, compare to simply html markup <table>? Am I getting the point? Commented Nov 13, 2010 at 7:18

3 Answers 3

6

Both Sébastien and Zain have valid points. It depends what kind of performance you're talking about.

If you want to reduce your server's bandwidth, then you should return JSON and create your display using client-side javascript.

However if your dataset is large, on most machines creating your display client-side could lag the browser and cause the UI to become unresponsive. If that is important to you then you might consider returning HTML from the server.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes you should return HTML if you don't have manipulation/interpretation to do with the result of your AJAX call.

4 Comments

Perhaps everyone on this thread forgot about the initial question which state : But not sure for the performance wise, which is better?
I don't see it like that, there is additionnal treatment time on the server AND on the client when you ENCODE() + DECODE() in JSON.
creating html on server will be more expensive than json. okay, give an example of in which format you would return the data from php page?
The question says "real case it would have a lot of data, may be in html table format."
0

no, I would not recommend returning html from php page. only return the json data and parse it on client side and show on UI. because html will always be heavier than json. so if you use json it will save your bandwidth.

In case you want an example, just go and check out what twitter is doing, they only return json and then manipulate json on client side.

5 Comments

Yes json will be the best option to get data from.
@uniq the advantage is you can maintain object hierarchy structure.
I think the main question is about performance. Returning bulk data, encapsuling in JSON and treating them in client side add a lot more treatment time in this particular case.
@Sébastien I am not talking about returning bulk data excapsulated in JSON. I am talking about returning pure json object and then manipulate it on client side.
Ok you are recommanding a Best Practice and I agree with your approach. In the particular case of this question I considered the original comment "real case it would have a lot of data, may be in html table format."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.