第一次 jQuery $.post 花费了非常长的时间,随后的时间正常

发布于 2024-12-05 13:32:05 字数 1276 浏览 0 评论 0原文

在网页上,我们有以下服务器端表单验证系统。例如,如果用户正在为某个事件添加日期详细信息(并且一个事件可以包含许多此类日期详细信息),我们将在单击“添加”按钮时调用 JavaScript 函数,如下所示。

validateForm('frmName','codelibrary/classes/myclass.php','validationArrName')
where:
@frmName = form name
@codelibrary/classes/myclass.php = location of class file, that contains classes and functions for server side validation
@validationArrName = Type of validation we apply

In the php script, validationArrName is defined as a list of keys (representing form fields) and values (representing the functions we will call to validate that form field).
validationArrName  = array ('fieldName1'=>validationFun1,'fieldName2'=>validationFun2);

eg:
fieldName1 = email_address
validationFun1 = validateEmail()

在html页面上,我们通过ajax调用服务器端验证,如下所示。

$.post(className,$("form[name="+formName+"]").serialize()+"&isValidate=1&validateArrayName="+validateArrayName,function(data){ ... });

如果验证函数报告错误,我们会在 html 页面上显示相应的错误消息。

问题是,当我们第一次执行此操作时(例如:在页面硬刷新后),提交此日期详细信息表单进行验证需要大量时间,与后续操作相比请求。

我们观察到,它不是调用一次 codelibrary/classes/myclass.php 文件,而是在跳转到所需位置 (validationArrName) 并运行该文件之前,实际上引用该文件超过 10 次。

对于后续请求,它工作正常并且仅引用该文件一次。

这里可能有什么问题?我们使用 jquery Submit 是否存在问题?

On a webpage we have the following system of server side form validation. For example, if the user is adding date-details for an event (and an event can contain many such date-details), we call a javascript function on click of the 'Add' button like below.

validateForm('frmName','codelibrary/classes/myclass.php','validationArrName')
where:
@frmName = form name
@codelibrary/classes/myclass.php = location of class file, that contains classes and functions for server side validation
@validationArrName = Type of validation we apply

In the php script, validationArrName is defined as a list of keys (representing form fields) and values (representing the functions we will call to validate that form field).
validationArrName  = array ('fieldName1'=>validationFun1,'fieldName2'=>validationFun2);

eg:
fieldName1 = email_address
validationFun1 = validateEmail()

On the html page, we call the server side validation through ajax as follows.

$.post(className,$("form[name="+formName+"]").serialize()+"&isValidate=1&validateArrayName="+validateArrayName,function(data){ ... });

If the validation function reports an error, we display an appropriate error message back on the html page.

The problem is that when we do this for the very first time (eg: after a hard refresh of the page), submitting this date-details form for validation takes a lot of time, as compared to subsequent requests.

We observed that instead of calling the codelibrary/classes/myclass.php file once, it actually refers to this file more than 10 times before jumping to the required location (validationArrName) and running that.

For subsequent requests, it works fine and refers to that file only once.

What could be the issue here? Could there be an issue with our usage of jquery submit ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-12-12 13:32:05

你能做的最好的事情就是时间。

在 javascript 中:

console.time('post load'):
$.post(className,$("form[name="+formName+"]").serialize()+"&isValidate=1&validateArrayName="+validateArrayName,function(data){ 
    console.timeEnd('post load');
    console.log('data');
    ... 
});

在 php 中,使用 microtime 对不同部分进行计时并回显它们。它们将打印在控制台中。

它不应该与缓存或包含相关,因为 ajax 每次都会启动一个新连接。

根据您的评论,我编辑了这个答案:

我仍然不知道发生了什么。然而我看到两种可能性。第一个是您使用“标志”来验证表单是否有效。当您加载页面时,所有表单标志都未设置,并且首先提交检查所有表单。后续提交工作正常。

另一种选择是,第一次提交表单时,您不会在提交点击时使用 event.preventDefault(),但这仍然是一个松散的解释。

我很想看看您如何调用 $.post(...) 函数(如何绑定提交按钮,或者如何调用 $().submit() )。

the best thing you can do is time stuff.

in javascript:

console.time('post load'):
$.post(className,$("form[name="+formName+"]").serialize()+"&isValidate=1&validateArrayName="+validateArrayName,function(data){ 
    console.timeEnd('post load');
    console.log('data');
    ... 
});

in php, use microtime to time different part and echo them. they will be printed in the console.

It should not be cache or include related, as ajax starts a new connection each time.

Following your comments, I edit this answer:

I'm still at loss of what happens. However I see two possibilities. The first one is that you use a "flag" to validate forms or not. When you load the page, all forms flag are unset, and first submit check them all. Subsequent submits works correctly.

Another option is that the first time you submit a form, you dont event.preventDefault() on the submit click, but it's still a loosy explanation.

I would love to see how you call the $.post(...) function (how the submit button is binded, or how $().submit() is called).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文