Force error return in ajax

1

For example when inserting the client (in PHP) validate if age is above 18, before inserting, validate with a condition, and if entering return an error, that in ajax, could do type:

 success: function (data) {
      alert('Sucesso');
 },
 error: function (data) {
      alert('Ocorreu um erro, verifique a idade informada!');
 }
    
asked by anonymous 01.10.2016 / 23:28

1 answer

0

The% ajax% is for cases where ajax has completed its execution correctly, sending / receiving data from the server and sucess is activated when an HTTP problem occurs (eg error 500).

If you want to do an old validation you can return a json in the backend with "error": "error message" or in case of success "success": "message of success", then you would perform the verification on the front by the parameter error of ajax.

    
02.10.2016 / 02:05