jQuery AJAX will trigger an error
event not only when it receives an HTTP status code indicating a problem with the request, but also if jQuery fails to parse the response body. Since you’re using die()
without sending any response body, it’s likely that jQuery is choking on the “empty” response.
Using wp_send_json_success()
instead of die()
should alleviate the issue.
// ...
$success = wp_mail($to2, $subject2, $message2, $headers2);
if( $success )
wp_send_json_success( null, 200 );
else
wp_send_json_error( array( 'message' => 'We\'re sorry, your message could not be sent at this time. Please contact an administrator' ), 500 );
}
Also consider implementing check_ajax_referer()
and using sanitization functions to increase the security of your form.
Related Posts:
- Add Server Side validation in Ajax mail form
- How to send automatic response after form submission without plugin
- Using AJAX on Contact-form the WordPress way
- jQuery’s .on() method combined with the submit event
- Displaying PHP Errors from admin-ajax.php
- Nonces and Cache
- How to HTML5 FormData Ajax
- admin-ajax.php doesn’t work when using POST data and Axios
- Contact Form 7 Custom Post Action
- Custom Form with Ajax
- ajax – why multiple calls to wp_create_nonce() return same value?
- How to link WordPress heartbeat to ajax form
- Ajax form submission from admin panel
- Confused on AJAX submit form through page template
- how to use reCaptcha v3 in wordpress custom login form?
- Prevent page reload after ajax form submission
- submitting form via admin-ajax.php returns 0
- Admin Ajax and HTML5 Formdata
- jQuery Ajax passing empty parameters to my function?
- Using ajax with wordpress
- Using AJAX with Forms
- How to solve “Updating failed” with 403 Error when trying to save a Site or Post in Gutenberg?
- Ajax image upload with media_handle_upload and form.js
- WordPress ajax success response
- (updated) How to add AJAX error handling to a (fully) custom registration form?
- wp_handle_upload returns empty error array
- Gravity form Load By Ajax Cannot Submit – Error 400
- Ajax post returning full html page as response
- eventON plugin AJAX call triggers website crash
- Sending variable from ajax on form submit
- Wp mail does not work when Ajaxed to
- Can’t trigger an AJAX function with a submit button in the dashboard
- wp_mail 200 response but no mail sent
- Dynamically add more fields/remove last field in a form
- Ajaxify Form That Submits To Same Page To Display Post Data [closed]
- Using admin-ajax prevents regular php form submission
- Specify ABSPATH in jQuery url
- ajax form is returning the dreaded “[HTTP/1.1 400 Bad Request” and a zero
- wp_mail doesn’t work when logged in?
- Output multi-steps form results in same page
- Using get_theme_mod in php ajax form doesn’t work
- How to create a form button that executes a function?
- How to stop being directed to admin.php after sending request to admin-ajax.php
- Contact form – ajax, wp_mail
- How to display contact form 7 form in vanilla js without jquery in frontend
- change wordpress pagination url after doing the request
- Should wordpress nonce be placed in html form or in javascript file
- Sending email through Ajax without page refresh hangs on admin_ajax.php
- WordPress POST AJAX call, var_dump($_POST) NULL and AJAX response empty when inside ob_start
- Opening Modal popup on Ajax form submission
- WordPress 403 error on form submission with Ajax
- admin-ajax.php 403 errors – no caching, permissions are fine
- Registration form AJAX check for existing username (simple version)
- Issue developing an AJAX form with WordPress
- Syntax error on query_vars while handling with Jquery
- Ajax show custom post data form & script
- How to send the checkbox value to email
- Sending email with wp_email and AJAX
- Error while submitting form using AJAX and php
- WordPress Ajax JSON success return no being recognized
- Ajax contact form returnig 0
- Ajax Form data is not posted back to the get_results()
- Using AJAX for dynamic settings pages
- While doing ajax, WordPress returning internal server error 500 and stating that my theme functions are undefined
- Custom Contact Form Not Sending (but not giving error)
- Ajax Form seems to post, but does not return
- Bad Request when adding new post via ajax form
- Data inserted in database, but ajax calls error function
- Send button using own contact form [closed]
- WordPress Custom wp mail template return full template
- Using Backbone with the WordPress AJAX API
- WordPress Ajax Data Security
- How do I hook an Ajax request into a PHP callback?
- How declare Ajax functions ussing SHORTINIT
- Load custom formatted comment with AJAX: reply link isn’t rendered?
- WP REST API route request explain
- WooCommerce: Translation lost on AJAX call in Checkout page [closed]
- Async Loading of Custom Posts
- How to add WP API and JS featured image attachment
- Ajax return code 400
- Can’t publish post using ajax
- Customizer AJAX using buttons
- Cache wp-json/posts without a plugin?
- Attach time/date stamp on add_post_meta
- infinite scroll not working when logged out
- Upload data from weather station to WordPress Website
- javascript variable to wordpress php variable
- How to reuse get_template_part() containing a simple loop to work with AJAX correctly?
- Need help with ajax
- Using nonce when loading posts with AJAX
- Tabbed feeds with ajax vs. without on homepage [closed]
- Not getting ajax success response on insert/update row to database
- Wp admin ajax load more
- Can’t get next post info with ajax
- How to inject data content from external json into a modal, using UIkit?
- Cannot access class properties from ajax call in wpordrpess
- why is my main.js not loading?
- Enqueue dynamically generated javascript
- First time doing Ajax with WP, how to do it?
- Adding custom fields to Wired Impact Volunteer Management Plugin