从AJAX调用操作数据:PHP还是Javascript?

这是一般性问题。 使用下面的示例,假设我有一个Javascript,它在PHP脚本上执行AJAX调用以从数据库中提取数据。 我可以选择:</ p>

a)</ strong>操纵PHP脚本本身的数据,然后再编码将其发送回Javascript,或者; </ p> \ n

 &lt;?php 
while($ row = mysqli_fetch_assoc($ result))
{
extract($ row);
array_push($ data,array('product'= &gt; $ product,'discount'=&gt; $ price * 0.15));
}
echo json_encode($ data);
?&gt;
</ code> </ pre>

< p> b)</ strong>从PHP脚本获取原始数据并对Javascript进行操作。</ p>

 &lt; SCRIPT type ='text / javascript'  &gt; 
$ .each(json,function(index,element){
element.discount = 0.15 * element.price
}
&lt; / SCRIPT&gt;
</ code> </ pre>
\ n

虽然我已经知道我可以使用选项b)获得更好的性能(服务器端),但我想听听社群是否是一个更明智的选择以及是否有任何逻辑论据反对它我可能 被忽视了。 谢谢!</ p>

注意:</ strong> </ p>


  • 这只是一个简单的场景,计算比较复杂
    than * 0.15。</ li>
  • Javascript是将数据和应用程序提取到
    work的必备工具。</ li>
    </ ul>
    </ div>

展开原文

原文

This is a general question. Using an example below, suppose that I have a Javascript that performs an AJAX call on a PHP script to pull data from a database. I can choose to:

a) manipulate the data on the PHP script itself before encoding an sending it back to the Javascript, OR;

<?php
 while ($row = mysqli_fetch_assoc($result))
 {
  extract($row);
  array_push($data, array('product' => $product, 'discount' => $price*0.15));
 }
 echo json_encode($data);
?>

b) get raw data from the PHP script and do the manipulation on the Javascript.

<SCRIPT type='text/javascript'>
 $.each(json, function(index, element) {
  element.discount = 0.15*element.price
 }
</SCRIPT>

Though I already know that I can get better performance (server-side) using option b), I would like to hear from the community whether it is a wiser choice and whether there are any logical arguments against it which I may have overlooked. Thanks!

NOTE:

  • This is just a simple scenario, the calculations are more complicated than *0.15.
  • Javascript is a must-have to pull data and for application to work.

dongzhe3171
dongzhe3171 它不能,如果它依赖谷歌地图等工作。
大约 9 年之前 回复
douba9776
douba9776 这就是为什么你在使用AJAX时应该总是有一个后备,所以在这种情况下你的网站仍然可以工作。
大约 9 年之前 回复
doudi8519
doudi8519 但由于AJAX肯定要依赖Javascript,如果关闭它就不会发送数据。
大约 9 年之前 回复
duanbi8529
duanbi8529 在客户端执行此类操作并不可靠,当您的JS停止工作时(因为用户已将其停用,或者由于浏览器的问题),您的站点无可救药地制动。此外,虽然您可以控制服务器的性能,但您无法知道哪种PC会打开您的页面
大约 9 年之前 回复

3个回答



在性能或执行时间方面,您不希望吝啬一些事情。 </ p>


  • 密码学就是其中之一。 </ li>
  • 任何具有财务方面的东西都是另一回事。 </ li>
    </ ul>

    我们可以通过JavaScript在客户端层上执行XYZ并不意味着我们应该被迫这样做。

    发生了什么</ p>

    A)如果关闭了JavaScript。
    B)您依赖于从POST发布的值。 (想象一下服务器使用$ _POST ['total_amount']的购物车。我要编辑POST或隐藏输入以获得免费的东西。</ p>

    如果是你的话 正在做的只是纯粹的信息,然后这并不重要,但如果你依赖任何信息,那么你应该多次检查价格。</ p>

    此外,当你想要的时候会发生什么 开始为您的服务提供API或其他东西。您的传输现在已被破坏,因为它不包含计算出的价格。</ p>

    服务器端,无论哪个层都更好。正如Duncan所说 ,SQL可以是一个选项,或者PHP。我对这个没有偏好,因为一小部分项目的任何收益都可以忽略不计。</ p>
    </ div>

展开原文

原文

Some things you do not want to be stingy on when it comes to performance or time taken to execute.

  • Cryptography is one of them.
  • Anything with a financial aspect to it, is another.

The fact that we can do XYZ on the client layer, through JavaScript doesn't mean that we should be compelled to do it. What happens

A) If JavaScript is turned off. B) You rely on the value POSTed from JavaScript. (Imagine a shopping cart which has $_POST['total_amount'] which is used by the server. I am going to edit that POST or hidden input to get free things.

If what you are doing is purely informative, then this does not matter as much, but if you rely on any information, then you should be checking the prices multiple times.

Additionally, what happens when you want to start providing an API or something to your service. Your transport is now broken because it doesn't contain the calculated prices.

DO it server side, at whichever layer better. As Duncan says, SQL could be one option, or PHP. I don't have a preference on this one since any gains for a smallset of items will be largely negligible.

dsfdsf23423
dsfdsf23423 不,它不会。 如果您只向用户提供信息详细信息,并且您从不依赖于这些值,那么最好为客户端提供。 这实际上取决于您的UX / UI以及您要实现的目标。 例如,用户可能希望能够根据折扣百分比自行调整总金额。 很明显,有了这个,你会让他们做客户端。 但是,您永远不想依赖于随后提交到您网站的值。
大约 9 年之前 回复
dpikoto468637
dpikoto468637 我喜欢你在密码学和财务方面的观点。 如果应用程序必须依赖javascript工作,并且不涉及$ _POST数据。 你的考虑仍然是一样的吗?
大约 9 年之前 回复



在SQL到达PHP之前,你最快在SQL中执行此操作。 如果javascript性能优于PHP性能,我也会感到惊讶(你怎么知道这对你更好?只是好奇......)。 尽可能多地服务器端通常是一个很好的经验法则。 您可以随时添加更多服务器等,但是您无法控制客户端性能。</ p>
</ div>

展开原文

原文

you'd be quickest doing this in your SQL before it even reaches your PHP. I'd also be surprised if the javascript performance is better than the PHP performance (how do you know this is better for you? Just curious...). Do as much as you can server-side is usually a good rule of thumb. You can always add more servers and so on, but there's not much you can do to control client-side performance.

douwen7905
douwen7905 我忘了添加服务器端性能。 这只是一个简单的场景,当然,计算比* 0.15更复杂。
大约 9 年之前 回复



2个选项的主要区别在于服务器时间,每次需要更改数据中的任何内容时,需要调用 严重影响性能的服务器(从用户的角度来看)。 </ p>

您可以采用混合方法:使用所有预先计算的数据构建您的页面,并在JS中使用相同的流程来更新公式
但我建议您这样做 ,因为你的开发成本加倍,而且你将面临爆炸性的维护工作,因为很难同时保持PHP和 JS一致。 </ p>

2个做出选择的一般建议:</ p>


  1. 我认为您不应该将性能点视为主要的

    &nconcern 但功能点。 制作最好的产品,然后优化它,因为如果不好的话,快速构建一些东西是没有意义的。</ li>

  2. 始终测量! 实施A&amp; B并比较结果。 它需要一些编码时间,但这是真正</ strong>回答你的问题的唯一方法。在这种情况下,这一点并不足以花时间进行AB测试,那么可能两个选项都是 在你的情况下是好的</ li>
    </ ol>
    </ div>

展开原文

原文

Main difference between the 2 options is that with server time, each time you need to change anything in your data, you need to call the server which is badly impacting performances (from user point of view).

You can have a mixed approach: build your page with all pre-calculated data and have the same process implemented in JS used to update the formulas when But I would advise you to do so, as you double your development costs and moreover you will face exploding maintenance effort as it is very hard to keep both PHP & JS consistent.

2 general advices to make your choice:

  1. I think you shouldn't consider the performance point as your main concern but the functional point. Make the best product and then optimise it as there's no point in building something fast if not good.
  2. Always measure! Implement A & B and compare results. It takes some coding time, but it's the only way to really answer your question.In the case this point isn't important enough to spend time on AB testing it, then it is probably that both options are as good in your case

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐