使用json的一个ajax调用,或者两个使用html的ajax调用? [英] using one ajax call with json, or two ajax calls with html?
问题描述
我有一个包含许多消息的元素。
每条消息都是一个HTML元素
< ; div class =items>
< other-non-related-elements ...>
< div class =item>
< h4>标题< / h4>
< p>内容< / p>
< / div>
< div class =item...
< / div>
这个内容会不断用ajax调用进行更新。
它们完成的方式:php代码正在生成一个准备好打印的html。并且JQuery代码将删除元素并添加新元素。
$。get('url',null,
$函数(数据){
$(。items .item)。remove();
$(。items)。append(data);
}
现在我需要添加一个(不是项目
)元素到< items>
元素。
它应该是这样的
<$ p $ < code>< div class =items>
< new element> content< / new element>
< other-non-related-elements ...>> ;
< div class =item>
< h4> title< / h4>
< p>内容< / p>
< / div>
< div class =item...
< / div>
为了保持它作为一个单一的Ajax调用,PHP将不得不生成一个json代码,jquery将操纵html元素。由于该页面已经非常忙于其他js代码,我不确定这是一个更好的解决方案。
另一个解决方案是做两个ajax调用。一个用于新元素
和一个物品。
任何建议?如果不是必要的话,不要担心客户端的性能。
做一个AJAX调用和使用客户端JS进行排序。
请记住,您的服务器必须处理大量请求,但Web浏览器只能处理几个请求。
因此,如果您可以随时将工作放在客户端 - 他/她不会注意到,但是他们一定会注意到,如果您的服务器不断遭到大量传入请求的轰炸。
抓住JSON并让JS执行工作。
I have an element that contain many messages.
each message is an HTML element
<div class="items">
<other-non-related-elements ...>
<div class="item">
<h4>title</h4>
<p>content</p>
</div>
<div class="item" ...
</div>
This content is being updated constantly with ajax calls. They way it's done: the php code is producing a ready-to-print html. and the JQuery code is removing the elements and appending the new ones.
$.get('url',null,
function(data){
$(".items .item").remove();
$(".items").append(data);
}
Now i need to add an single (not item
) element to the <items>
element.
it should be like this
<div class="items">
<new element>content</new element>
<other-non-related-elements ...>
<div class="item">
<h4>title</h4>
<p>content</p>
</div>
<div class="item" ...
</div>
In order to keep it as a single ajax call, the php will have to produce a json code, that the jquery will manipulate into html elements. since the page is already very busy with other js code, i'm not sure that it's a better solution.
the other solution is to make two ajax calls. one for the new-element
and one fot the items.
Any advice? ideas?
Don't bother about client performance if not necessary.
Make one AJAX call and sort things out with client-side JS.
Remember that your server has to process lots of requests but the web browser has to deal with only a couple of requests.
So if you can always put the job on the client-side - he/she won't notice but they'll definitely notice if your server is constantly bombarded with lots of incoming requests.
Grab JSON and let JS do the job.
这篇关于使用json的一个ajax调用,或者两个使用html的ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!