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 make Contact Form 7 work when injected via AJAX in WordPress?
- Catch Form value at AJAX Form submit
- WordPress Ajax send response on every iteration of a loop
- get_template_part() does not render after the ajax request
- Translating wordpress foreach to ajax
- WordPress Ajax – looping data into a table
- $wpdb Ajax not redirecting to main page
- AJAX loading with custom parameters
- Front-end ajax problem all users and guests getting a 302 redirect when accessing wp-admin/admin-ajax.php
- Use Ajax to get an Options Settings Value
- how reduce fetch/XHR response time
- WordPress error
- Running js in html code with same content
- How to get current_user_id from wordpress in node js?
- Custom form validation
- How to get data with Select AJAX PHP
- Adding custom fields to Wired Impact Volunteer Management Plugin
- wordpress ajax bad request 400
- Add “load more” functionality to an AJAX response
- how to update pagination after ajax call page 2 show 0
- Custom WP rest api endpoint only working on non https?
- how can i create get request unis ajax without expecting return value
- Placing ajax actions in different class
- Bad Request 400… jQuery ajax post of json data to wordpress admin-ajax.php
- Access to a data from a response AJAX called in a template file php
- How to update my jquery/PHP function to add/remove user as favorites in (WordPress) users list
- AJAX call not initializing for non-admins in WordPress
- Can’t have two simultaneous charts using Apex Charts
- Workaround AJAX calling without directly calling WordPress backend (Admin dashboard) because it is restricted to the end user (the public)