简体   繁体   English

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

[英]Return '0' or 0 on success from AJAX call?

I have a series of nested AJAX calls that validate each element of a form that's being submitted. 我有一系列嵌套的AJAX调用,用于验证正在提交的表单的每个元素。 Each one checks the result of a data variable created by the PHP file that's called. 每个都检查由调用的PHP文件创建的data变量的结果。 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. 如果PHP例程不成功,则程序echoes由AJAX例程,这又在警告框中呈现捕获的错误信息为用户看到,这样的形式可以根据需要进行修正,然后重新提交。 If the PHP routine is successful, I echo 0 and the next AJAX validation script is called. 如果PHP例程成功,我echo 0并调用下一个AJAX验证脚本。 Only once all of the form elements validate successfully will the form be processed. 只有在所有表单元素成功验证后,才会处理表单。

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计算接收到的data变量时,会发生类型强制。

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)使用data !== '0'评估data !== '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. 我可能会在这里拆分,但我更倾向于使用一种技术,它采用解释器处理步骤最少的数量,并且可能有一种更有效的方式来返回成功值而不是我设置它的方式,或者我列出的3种可能的替代品中的任何一种。 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! 非常感谢!

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.除非你使用JSON,否则它无法告诉接收JavaScript它是0或'0' - 处理清楚否定你想要做的任何重铸优化。

So, the JS will always receive a string. 所以,JS总会收到一个字符串。 Knowing this, use the appropriate comparison to avoid coercion on the client side !== '0' 知道这一点,使用适当的比较来避免客户端的强制!== '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. 另一方面,如果你要发送一百万个AJAX请求,这样的收益可能是你需要担心的最后一件事。

 **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 );

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

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