json - Javascript object declaration in IF statement -
i'm sorry title i'm not sure how describe one.
basically i'm using jquery/ajax invoke php script validate input , data. returned data encoded json, set datatype json in jquery , returns object.
i have discovered under (it has said unusual) conditions network connection not responding in expected way, ajax call invokes error option in jquery.
in cases have error message coming server, error reached , no json has been sent server side script. haven't tracked down can provoke network situation yet, in case thought i'd deal in javascript whilst don't yet know.
the idea check see if expected object had been created, , if not create 2 expected properties , run dialogue. way i'd avoid repeating writing error messages. it's not big saving, wanted try principle.
$.ajax({ url: 'getmetadata.php', type: "post", data: entereddata, datatype: "json", timeout: (7000), //wait 7 seconds error: function(data) { // handle errors if(data) { // nothing error message issued data object }else{ // no message returned. other error occured. function tempdata() { this.errortitle = "error title"; this.errormessage = "error text"; }; var data = new tempdata(); }; // issue error message rundialogue(data.errortitle,data.errormessage); }, //error success: function(data) { }; // success }); // ajax in code above, data object should either exist or not before "if" statement. , when rundialogue(); object data should exist in order pass errortitle , errordescription properties.
when "data" object exists, there no problem. isn't working when "data" object not exist, ie if fails "if" test. else should create object "data" doesn't.
what have done wrong?
you redefining variable data in else block(more specific scope) , hence global scope variable still undefined. change var data = new tempdata(); to
data = new tempdata(); //in else block so, ajax call should like:
$.ajax({ url: 'getmetadata.php', type: "post", data: entereddata, datatype: "json", timeout: (7000), //wait 7 seconds error: function(data) { // handle errors if(data) { // nothing error message issued data object }else{ /******************note change here********************/ data = { errortitle:"error title", errormessage:"error text" }; }; // issue error message rundialogue(data.errortitle,data.errormessage); }, //error success: function(data) { }; // success }); // ajax
Comments
Post a Comment