简体   繁体   English

为什么 AJAX 这么长?

[英]Why does AJAX is so long?

Hi all i'm trying to compile and send some forms to a php file, but i noticed that it's very slow, even if in PHP i var_dump only the POST values, it takes the same time if i run with my correct functions.大家好,我正在尝试编译一些表单并将其发送到 php 文件,但我注意到它非常慢,即使在 PHP 中我 var_dump 仅 POST 值,如果我使用正确的函数运行它也需要相同的时间。 Maybe the code is wrong, this is what i'm trying to do:也许代码是错误的,这就是我想要做的:

$("#button").click(function(){
        $("#button").css("border","2px solid blue");
        var total_forms = $("input[name=total_forms]").val();
         $.ajax( {
        type: "POST",
        url: "<? echo "myurl.php";?>",
        data: {delete : 1}, //before sending my forms i need to run another function that delete some items in mySQL DB
        success: function(data) {
        for(var i = 0; i < total_forms; i++){
            var string_ok = "#form_number_";
            string_ok = string_ok.concat(i);
            var dataString = $(string_ok).serialize();
             $.ajax( {
        type: "POST",
        url: "echo "myUrl.php;"",
        data: dataString,
        success: function(data) {
            console.log(data); //it's a bit faster, only a bit
            $("#mydiv").append(data); //it's a bit slower
        }
            });
        }
        }
            });
    });

I forget to say that i'm running it on MAMP in a macbook with 8GB RAM and SSD with only 10GB free, maybe my PC is the problem, IDK..我忘了说我在 macbook 上运行它,它有 8GB RAM 和 SSD,只有 10GB 免费,也许我的电脑是问题,IDK..

it takes about 4minutes to compile, send to PHP and stamp datas for 500forms编译,发送到PHP并为500个表单标记数据大约需要4分钟

this is an example of dataString that i send:这是我发送的 dataString 示例:

prd_number=1259&prd_max_number=1763&old_prd_id=0&prd_action=true&title=INFORMAT%20G1X85A%23A80&title_check=true&old_title_text=&new_image_url=&old_image_url=&is_mech=&image_check=true&new_cat=Informatica%2CINFORMAT&category=true&new_price_text=222.04&price=true&new_sku_text=888182658918&oth=true&ncw=negat&stwe=testing&mark_text=INFORMAT_G1X85A%23A80&check_two=true&other_sto=table&var2=true&elemt=&weight=true&qt=3&qslow=true&clients=Norman%2Cli&taking=true&setting=normal&vars_set=true

i noticed that if i console.log(dataString), the console first stamps all dataString logs, and then starts to print each php response.我注意到,如果我使用 console.log(dataString),控制台首先会标记所有 dataString 日志,然后开始打印每个 php 响应。

  • Are you sending the data locally?你是在本地发送数据吗? Like is your php server running locally or is it hosted somewhere?就像您的 php 服务器是在本地运行还是托管在某个地方?
  • How much data are on the forms?表格上有多少数据?
  • Also your code is sending an ajax request in the for loop.此外,您的代码正在 for 循环中发送 ajax 请求。 so You have a nested ajax call going on, one on every iteration of your loop.所以你有一个嵌套的ajax调用,在你的循环的每次迭代中调用一次。 Thats of course going to be slower, is there a way you can refactor this to be one ajax request?那当然会变慢,有没有办法将其重构为一个ajax请求? Basically your making an ajax request, waiting for that response to come back, for looping, then sending another every time you iterate.基本上你发出一个ajax请求,等待那个响应回来,循环,然后每次迭代时发送另一个。 Seems a little convoluted.好像有点绕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM