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
- WordPress REST API FormData: Form Not Submitted When No Files Attached
- How to get a unique nonce for each Ajax request?
- Open a Thickbox with content trough AJAX
- AJAX only works when I’m logged as Admin
- When using javascript to dynamically add a textarea, how can I render it as a tinyMCE editor (in wordpress)?
- Drag and drop multiple file upload using Ajax WordPress
- How to “Load More” posts via AJAX?
- Contact forms going into spam folder
- Show Post Content with AJAX
- update_post_meta using ajax
- WordPress Ajaxify Problem with Scroll-to [closed]
- Can you disable the mail function in contact form 7 [closed]
- Stop ajax listener from sending empty parameter to function
- 400 Bad Request when sending XHR from React.js to admin-ajax.php
- What exactly does ‘authenticated’ mean for wp_ajax_nopriv?
- Ajax mousedown call getting “cancelled” when clicking link
- Get uploaded attachment width & height and attachment ID after upload them
- Refresh checkout fields on add to cart with order bump
- Is it good practice to use REST API in wp-admin plugin page? [closed]
- How to check Ajax request only when i opened the Notifications list?
- All WordPress AJAX calls always return 0
- Class called in template, AJAX not registering
- Same query in category.php and in function.php, but different result
- Ajax request to admin-ajax.php and window.location.href
- Objects and get_post_meta() gives me 1 string for latitude and longitude
- WordPress ajax requst returns zero
- wp_editor() in content that was loaded with ajax [duplicate]
- WordPress blocking polling request when signed into Admin
- Workaround AJAX calling without directly calling WordPress backend (Admin dashboard) because it is restricted to the end user (the public)
- Code understanding