Web to lead from "success" and "error" response

Does the web to lead form return any messages after POST? I am trying to display “sucess” and “error” messages. Right now an “error” message is displayed even though data is successfully captured. Is there something I am doing wrong or need to add? Here is the javascript:

$.ajax({
					type: 'POST',
					url: "http://change.this.domain/public/index.php?entryPoint=WebToPersonCapture",
					data: data
				}).always(function(data, textStatus, jqXHR) {

					$errorMessage.empty().hide();

					if (data.response == 'success') {

						// Uncomment the code below to redirect for a thank you page
						// self.location = 'thank-you.html';

						$messageSuccess.removeClass('d-none');
						$messageError.addClass('d-none');

						// Reset Form
						$form.find('.form-control')
							.val('')
							.blur()
							.parent()
							.removeClass('has-success')
							.removeClass('has-danger')
							.find('label.error')
							.remove();

						if (($messageSuccess.offset().top - 80) < $(window).scrollTop()) {
							$('html, body').animate({
								scrollTop: $messageSuccess.offset().top - 80
							}, 300);
						}

						$form.find('.form-control').removeClass('error');

						$submitButton.val( submitButtonText ).attr('disabled', false);
						
						return;

					} else if (data.response == 'error' && typeof data.errorMessage !== 'undefined') {
						$errorMessage.html(data.errorMessage).show();
					} else {
						$errorMessage.html(data.responseText).show();
					}

					$messageError.removeClass('d-none');
					$messageSuccess.addClass('d-none');

					if (($messageError.offset().top - 80) < $(window).scrollTop()) {
						$('html, body').animate({
							scrollTop: $messageError.offset().top - 80
						}, 300);
					}

					$form.find('.has-success')
						.removeClass('has-success');
						
					$submitButton.val( submitButtonText ).attr('disabled', false);

				});

Hi, welcome to the Community! :tada:

I think the entry-point is not designed to be called as Ajax.

It seems to have code for redirects at the end, but nothing to return values. Have a look:

https://github.com/salesagility/SuiteCRM/blob/master/modules/Campaigns/WebToLeadCapture.php

Thanks for the response. I was able to get it working. I modified webtopersoncapture.php beginging at line 342 with the following:

    } else {
        if (isset($mod_strings['LBL_THANKS_FOR_SUBMITTING'])) {
            echo json_encode(['response' => 'success']);
        } else {
            if (isset($errors) && $errors) {
                $log = LoggerManager::getLogger();
                $log->error('Success but some error occurred: ' . implode(', ', $errors));
            }
                      header($_SERVER['SERVER_PROTOCOL'].'201', true, 201);
    		}

            //If the custom module does not have a LBL_THANKS_FOR_SUBMITTING label, default to this general one
            echo json_encode(['response' => 'success']);
           }
    sugar_cleanup();

Here is the JavaScript to parse JSON objects:

			// Ajax Submit
            $.ajax({
                type: 'POST',
				url: "https://domain.name.here/public/index.php?entryPoint=WebToPersonCapture",
				data: data,
				success: function(response) {
                    var data = JSON.parse(response);
					console.log(data);
            
                    $errorMessage.empty().hide();
            
                    if (data.response == 'success') {
                        $messageSuccess.removeClass('d-none');
                        $messageError.addClass('d-none');
            
                        // Reset Form
                        $form.find('.form-control')
                            .val('')
                            .blur()
                            .parent()
                            .removeClass('has-success')
                            .removeClass('has-danger')
                            .find('label.error')
                            .remove();
            
                        if (($messageSuccess.offset().top - 80) < $(window).scrollTop()) {
                            $('html, body').animate({
                                scrollTop: $messageSuccess.offset().top - 80
                            }, 300);
                        }
            
                        $form.find('.form-control').removeClass('error');
            
                        $submitButton.val( submitButtonText ).attr('disabled', false);
            
                        return;
            
                    } else if (data.response == 'error' && typeof data.errorMessage !== 'undefined') {
                        $errorMessage.html(data.errorMessage).show();
                    } else {
                        $errorMessage.html(data.responseText).show();
                    }
            
                    $messageError.removeClass('d-none');
                    $messageSuccess.addClass('d-none');
            
                    if (($messageError.offset().top - 80) < $(window).scrollTop()) {
                        $('html, body').animate({
                            scrollTop: $messageError.offset().top - 80
                        }, 300);
                    }
            
                    $form.find('.has-success')
                        .removeClass('has-success');
            
                    $submitButton.val( submitButtonText ).attr('disabled', false);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    // Handle error here
                }

			});
		}
	});
});

Nice. And would this now work for both situations? I mean, your changes enable Ajax calls, but does the entry point still work correctly if called without Ajax, returning the redirected web page?

If so, then you should add your code to GitHub so everyone can benefit.