javascript - Return '0' or 0 on success from AJAX call? -


i have series of nested ajax calls validate each element of form that's being submitted. each 1 checks result of data variable created php file that's called. if php routine not successful, routine echoes error message captured ajax routine, in turn presented in alert box user see form can corrected necessary , resubmitted. if php routine successful, echo 0 , next ajax validation script called. once of form elements validate form processed.

my question has our dear friends != operator , !== operator, regards efficiency of operation performance (i understand difference in how function). currently, routines structured this:

in jquery/ajax:

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

and @ end of data_check.php (and such php routines called parent ajax routine):

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

this works. however, i'm aware echo command in php file produces string value received ajax routine, , therefore type coercion takes place when received data variable evaluated data != 0.

i revise current routine with:

(1) echoing '0' instead of 0 (in php, eliminate type-casting php (???) still rely on type-coercion in ajax), or:

(2) evaluating data data !== '0' (in ajax, eliminate type coercion, still rely on type-casting php (???)), or:

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

but which--if any--of these methods efficient? may splitting hairs here, i'd prefer employ technique employs fewest number of processing steps interpreter, , there may more efficient way return success value way i've set up, or of 3 possible replacements i've listed. please let me know of different ways code (including ways haven't listed) efficient processing-wise. many thanks!

i think getting mixed here. there no coercion php @ all, job output 0. there's no way tell receiving javascript 0 or '0' unless use json - processing of negate recasting optimisations you're trying make.

so, js receive string. knowing this, use appropriate comparison avoid coercion on client side !== '0'

having said that, classic example of premature optimisation. if perform action 1 million times, might able record minute performance gain. on other hand, if sending 1 million ajax requests, such gain last thing need worrying about.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -