从AJAX调用成功返回'0'或'0' ?

时间:2022-10-08 08:43:14

I have a series of nested AJAX calls that validate each element of a form that's being submitted. Each one checks the result of a data variable created by the PHP file that's called. If the PHP routine is not successful, the routine echoes an error message captured by the AJAX routine, which in turn is presented in an alert box for the user to see so that the form can be corrected as necessary and then resubmitted. If the PHP routine is successful, I echo 0 and the next AJAX validation script is called. Only once all of the form elements validate successfully will the form be processed.

我有一系列嵌套的AJAX调用来验证提交表单的每个元素。每一个都检查由PHP文件创建的数据变量的结果。如果PHP例程不成功,则该例程响应由AJAX例程捕获的错误消息,该错误消息将在一个警告框中显示,以便用户查看,以便在必要时纠正表单,然后重新提交。如果PHP例程成功,我将回显0,并调用下一个AJAX验证脚本。只有所有的表单元素成功验证后,才能对表单进行处理。

My question has to do with our dear friends the != operator and the !== operator, but regards efficiency of operation performance (I understand the difference in how they function). Currently, the routines are structured like this:

我的问题与我们亲爱的朋友有关!=操作符和!==操作符,但是关于操作性能的效率(我理解它们的作用方式的不同)。目前的例程结构如下:

In the JQuery/AJAX:

在JQuery / AJAX:

$.get('data_check.php', { id: $('#id').val() }, function(data) {
    if (data != 0) {
        alert(data);
    } else {
        [...next AJAX call...]

And at the end of data_check.php (and all such PHP routines called by the parent AJAX routine):

在data_check的末尾。php(以及父AJAX例程调用的所有此类php例程):

if (!$success) {
    echo $error_message;
    exit;
} else {
    echo 0;
    exit;
}

This works. However, I'm aware that the echo command in the PHP file produces a string value that is received by the AJAX routine, and therefore type coercion takes place when the received data variable is evaluated with data != 0.

这个作品。但是,我知道PHP文件中的echo命令会产生一个由AJAX例程接收的字符串值,因此,当用data != 0计算接收到的数据变量时,就会发生类型强制。

I could revise the current routine with:

我可以用:

(1) echoing '0' instead of 0 (in the PHP, which will eliminate type-casting by the PHP (???) but still will rely on type-coercion in the AJAX), or:

(1)响应“0”而不是0(在PHP中,这将消除PHP的类型转换,但仍然依赖于AJAX中的类型强制),或者:

(2) evaluating the data with data !== '0' (in the AJAX, which would eliminate type coercion, but still would rely on type-casting by the PHP (???)), or:

(2)使用数据对数据进行评估!= '0'(在AJAX中,这将消除类型强制,但仍然依赖于PHP(???)的类型转换),或:

(3) doing both of the above (which should most definitely eliminate type coercion).

(3)同时进行上述两种操作(最应该消除类型强制)。

But which--if any--of these methods is the most efficient? I may be splitting hairs here, but I'd prefer to employ a technique that employs the fewest number of processing steps by the interpreter, and there may be a more efficient way to return a success value than the way I've set it up, or with any of the 3 possible replacements I've listed. Please let me know which of the different ways to code this (including ways I haven't listed) is most efficient processing-wise. Many thanks!

但是这些方法中哪一个是最有效的呢?我在这里可能有点小题大作,但是我更喜欢使用一种技术,它使用最少数量的处理步骤的翻译,有可能是一个更有效的方法返回一个成功价值比我设置它的方式,或与任何3我列出可能的替代品。请告诉我有哪些不同的编码方式(包括我没有列出的方式)是最有效的处理方式。很多谢谢!

2 个解决方案

#1


3  

I think you are getting mixed up here. There would be no coercion by PHP at all, its job is simply to output a 0. There's no way for it to tell the receiving JavaScript that it is a 0 or a '0' unless you use JSON - the processing of which would clearly negate any recasting optimisations you're trying to make.

我觉得你搞混了。PHP根本不需要强制,它的工作只是输出一个0。它没有办法告诉接收的JavaScript是0或0,除非您使用JSON——这样的处理将明显地否定您正在尝试的任何重做优化。

So, the JS will always receive a string. Knowing this, use the appropriate comparison to avoid coercion on the client side !== '0'

所以,JS总是会接收一个字符串。了解这一点,使用适当的比较来避免在客户端强制!= '0'

Having said that, this is a classic example of premature optimisation. If you were to perform the action one million times, you might be able to record a minute performance gain. On the other hand, if you are sending one million AJAX requests, such a gain is likely the last thing you need to be worrying about.

话虽如此,这是过早优化的一个经典例子。如果您要执行该操作一百万次,您可能可以记录一分钟的性能增益。另一方面,如果您正在发送100万个AJAX请求,那么这样的增加很可能是您最不需要担心的事情。

#2


0  

 **php**

 $responseMessage = array('success' => true, 'message' => 'Your validation error/errors');
 header('Content-Type: application/json');
 echo json_encode($responseMessage);
 exit();

**javascript**

$.get('URI', function(data) 
{
if (data.success != undefined) 
{
   if (data.success) 
   {
       //show successful message
   }
   else 
   {
       $(form).find('#form-error-container').append(data.message);
       //handle the validation errors
   }
}
else 
{
   //handle via ajaxError/.fail or here :(
}
});

To add more parameters just expand the array;

要添加更多参数,只需展开数组;

 $responseMessage = array('success'=>true, 'errors'=>array('username'=>'Too many characters'));

 **js**
 data.errors.forEach( //your logic );

#1


3  

I think you are getting mixed up here. There would be no coercion by PHP at all, its job is simply to output a 0. There's no way for it to tell the receiving JavaScript that it is a 0 or a '0' unless you use JSON - the processing of which would clearly negate any recasting optimisations you're trying to make.

我觉得你搞混了。PHP根本不需要强制,它的工作只是输出一个0。它没有办法告诉接收的JavaScript是0或0,除非您使用JSON——这样的处理将明显地否定您正在尝试的任何重做优化。

So, the JS will always receive a string. Knowing this, use the appropriate comparison to avoid coercion on the client side !== '0'

所以,JS总是会接收一个字符串。了解这一点,使用适当的比较来避免在客户端强制!= '0'

Having said that, this is a classic example of premature optimisation. If you were to perform the action one million times, you might be able to record a minute performance gain. On the other hand, if you are sending one million AJAX requests, such a gain is likely the last thing you need to be worrying about.

话虽如此,这是过早优化的一个经典例子。如果您要执行该操作一百万次,您可能可以记录一分钟的性能增益。另一方面,如果您正在发送100万个AJAX请求,那么这样的增加很可能是您最不需要担心的事情。

#2


0  

 **php**

 $responseMessage = array('success' => true, 'message' => 'Your validation error/errors');
 header('Content-Type: application/json');
 echo json_encode($responseMessage);
 exit();

**javascript**

$.get('URI', function(data) 
{
if (data.success != undefined) 
{
   if (data.success) 
   {
       //show successful message
   }
   else 
   {
       $(form).find('#form-error-container').append(data.message);
       //handle the validation errors
   }
}
else 
{
   //handle via ajaxError/.fail or here :(
}
});

To add more parameters just expand the array;

要添加更多参数,只需展开数组;

 $responseMessage = array('success'=>true, 'errors'=>array('username'=>'Too many characters'));

 **js**
 data.errors.forEach( //your logic );