简体   繁体   中英

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. 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.

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:

$.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):

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 .

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:

(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:

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

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!

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.

So, the JS will always receive a string. Knowing this, use the appropriate comparison to avoid coercion on the client side !== '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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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