Home:ALL Converter>Ajax success function works even if the there is no success

Ajax success function works even if the there is no success

Ask Time:2015-08-29T15:20:32         Author:shubham sharma

Json Formatter

$something=100;
$.ajax({
          type: "post",
          url: "some.php",
          data: somevariable,
          dataType: "text",                  
          success: function(data) {         
            $something=$something-10; 
            alert($something);
          }.bind(this)
      });

Above ajax is used to fetch data from some.php file which is used to fetch data from mysql database. The file alerts a message when there is success or data retrieved from database but even if i stop mysql server and then again call the ajax then also it alerts the message with decrement in value of $something

So how can i make it work like if there is no row returned from database or empty row then there should be no success that is success only if there is some actual data returned from the database?

Author:shubham sharma,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32283648/ajax-success-function-works-even-if-the-there-is-no-success
sje397 :

How about something like:\n\nfunction fail() {\n alert(\"Fail!\");\n}\n$something=100;\n$.ajax({\n type: \"post\",\n url: \"some.php\",\n data: somevariable,\n dataType: \"text\", \n success: function(data) {\n if(!data) return fail(); \n $something=$something-10; \n alert($something);\n }.bind(this)\n }).fail(fail);\n\n\nYou might need to check data more thoroughly depending on how it's formatted.\n\nHowever, it's probably better if you can to get the server to not return a 200 (or another non-error code) if there's no data.",
2015-08-29T07:24:47
palaѕн :

You can do something like this:-\n\nsuccess: function (data) {\n // Checking data is not null or undefined and valid data is returned\n if (data && data.length > 0) {\n $something = $something - 10;\n alert($something);\n }\n}\n\n\nAssuming that data is an array of data returned from server. \n\nAlso, to be safe use deferred jqXHR.fail() like\n\n$.ajax(\"some_unknown_page.html\")\n.fail(function (jqXHR, exception) {\n var msg = '';\n if (jqXHR.status === 0) {\n msg = 'Not connect.\\n Verify Network.';\n } else if (jqXHR.status == 404) {\n msg = 'Requested page not found. [404]';\n } else if (jqXHR.status == 500) {\n msg = 'Internal Server Error [500].';\n } else if (exception === 'parsererror') {\n msg = 'Requested JSON parse failed.';\n } else if (exception === 'timeout') {\n msg = 'Time out error.';\n } else if (exception === 'abort') {\n msg = 'Ajax request aborted.';\n } else {\n msg = 'Uncaught Error.\\n' + jqXHR.responseText;\n }\n $('#post').html(msg);\n});\n\n\nThis will help you to figure out the specific error that occurred while returning the data from server, and after that you can take appropriate action.\n\nOr, as a simple example:-\n\n// Assign handlers immediately after making the request,\n// and remember the jqXHR object for this request\nvar jqxhr = $.ajax( \"example.php\" )\n .done(function() {\n alert( \"success\" );\n })\n .fail(function() {\n alert( \"error\" );\n })\n .always(function() {\n alert( \"complete\" );\n });\n\n// Perform other work here ...\n\n// Set another completion function for the request above\njqxhr.always(function() {\n alert( \"second complete\" );\n});\n",
2015-08-29T07:25:02
yy