Error message back in ajax

I have the following code sending my form data to upload.php via ajax. Inside the php file, the server checks the size / format of the downloaded file, etc. How can I tell ajax if this happened and stop the success message?

$.ajax({ type: 'POST', url: 'upload.php', data: dataString, success: function() { $('#getintouch .alert-error').slideUp(); $('#getintouch .alert-success').slideDown(); $('#submit').data('loading-text', 'Sent'); $('#submit').button('loading'); } }); 
+4
source share
1 answer

There are two ways to do this:

  • Ask the server to respond to the message and check it for JavaScript.

     success: function( data ) { if ( !data.success ) //if JSON { // do error message return; } 
  • Change the HTTP response code

     http_response_code( 500 ); 

    EDIT - use the error parameter at the front end to handle it:

     error: function(){...}, success: function(){...}, 
+2
source

Source: https://habr.com/ru/post/1484040/


All Articles