dsgni26260 2016-02-13 04:18
浏览 47
已采纳

Ajax用php服务器调用性能

When I have to get something from my server and pass some value to it I'm currently using $.post('myserver/formhandler.php', { 'v' : value }), and in my formhandler.php file I have the following:

require_once("mainclass.php");
require_once("config.php"); 

if(isset($_POST['v'])) {
    $mainclass->FunctionA($_POST['v']);
}
if(isset($_POST['s'])) {
    $mainclass->FunctionB($_POST['s']);
}

and so on. The thing is that sometimes I want to load something without passing any value, and my doubt is if it's faster do use the same $.post() function and pass a variable just to be able to trigger some function in the same file or if it would be better to use $.load() and have one specific file for each function, like $.load('LoadFunctionC.php'), where LoadFunctionC.php would be something like:

require_once("mainclass.php");
require_once("config.php"); 

$mainclass->FunctionC();

Would it slow down my website for, for example, calling mainclass.php and config.php multiples times? Thanks!

  • 写回答

1条回答 默认 最新

  • doushou8299 2016-02-13 04:58
    关注

    If you look at the $.load docs, you will see that $.load, just like $.post and $.get (and a few others) are really just calling $.ajax. Thus, there shouldn't be any significant performance change among them.

    A better way to do this might be to give each endpoint its own file. That way you just choose in the JS which endpoint to hit. Why make your server do work (and make your scripts more complicated) when you can get the user's browser to do the work for you!

    var url = '';
    switch (someCase) {
      case 'v':
        url = 'doThingV.php';
        break;
      case 's':
        url = 'doThingS.php';
        break;
      default:
        url = 'doThingC.php';
    }
    
    $.ajax({
      url: url,
      ...
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?