简体   繁体   English

为什么ajax调用非常慢?

[英]Why ajax call is very slow?

I was calling an ajax function on load page event. 我在加载页面事件中调用了ajax函数。 When I load the page, in the server log, There was not existing call log(I log entrance and out on all mvc server method). 当我加载页面时,在服务器日志中,没有现有的调用日志(我在所有mvc服务器方法上都记录入口和出口)。 The request from javascript to server was taking some time, 2 to 3 minutes. 从javascript到服务器的请求花费了2到3分钟的时间。 The strange thing is when I test it local and test server, it works well. 奇怪的是,当我在本地测试服务器并对其进行测试时,它运行良好。 It occurs just when I deploy a project to the real server! 仅当我将项目部署到真实服务器时才会发生!

I found some posts about the method to do ajax, xmlhttp, $.ajax(). 我发现了一些有关做ajax,xmlhttp,$。ajax()方法的帖子。 I already used both. 我已经用过了。 But It still exists. 但是它仍然存在。

This is my javascript code. 这是我的JavaScript代码。

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: allservicesUrl,
        async: true,
        success: function(result, status, xhr){
            var services = JSON.parse(xhr.responseText);
            for (i in services) {
                createServicecard(services[i]);
            }
        },
        error: function(xhr, status, err) {
            alert(xhr.responseText);
        }
    });
})

I wanna execute it immediately. 我想立即执行它。 How do I correct this problem? 我该如何解决这个问题?

Thanks for the advice. 谢谢你的建议。

I saw an answer and comments. 我看到了答案和评论。 Then, I saw the dev tool(network tab), finally, I found the problem. 然后,我看到了开发工具(网络选项卡),最后,我发现了问题。

Now I correct it, it works well. 现在,我更正了,它运作良好。 Thank you very much. 非常感谢你。

ps. ps。 Problem: connecting the internet in closed network. 问题:在封闭的网络中连接互联网。

Use the debugging/developer tools in your browser to trouble shoot this issue. 使用浏览器中的调试/开发工具解决此问题。 look in the console and see if you have any JS errors, then look in the network tab. 在控制台中查看,看看是否有JS错误,然后在“网络”标签中查看。 Clear the entries and then reload the AJAX call. 清除条目,然后重新加载AJAX调用。

you should be able to see if your script is slow to send the request to the server or if the server is slow to answer. 您应该能够看到脚本将请求发送到服务器的速度是否缓慢,或者服务器的响应速度是否缓慢。

until you figure out if the bottleneck is in the script or on the server you can't fix it. 除非您确定瓶颈是在脚本中还是在服务器上,否则您将无法修复它。

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

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