I want to use the ajax capability of not refreshing the page or rerouting the user to the registration page. The site has loginLogin / registerRegister dialogue modals have been created. I have the login javascript ajax working perfectly using an ajax call to the login api and Iwithout any configurations or added modules. I want to integrate the registration form in the same way. I would like the registration to:
Ajax Post is returning '<' obviously this is returning some html. How do I know how to pass the data in the ajax call and what to expect as a response?
My example below:
<style>
.scroll {
//background-color: #fed9ff;
//width: 600px;
height: 500px;
overflow-x: hidden;
overflow-y: auto;
//text-align: center;
//padding: 20px;
}
.close {
margin: 10px 25px 0px;
}
#log-title {
margin-left:25px;
}
</style>
<a data-target="#register-modal" data-toggle="modal" class="" id="" href="#register-modal">Register / Create an Account </a>
<div id="overlay_container"></div>
<!-- Register to download button so stays on same page. -->
<div id="register-modal" class="modal" tabindex="-1" role="dialog" aria-hidden="true">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 id="log-title">Please register:</h3>
<div class="modal-dialog">
<div class="modal-content scroll">
<?php
$register_form = \Drupal::service('entity.form_builder')->getForm(\Drupal\user\Entity\User::create([]), 'register', []);
// Load the service renderer
$render = Drupal::service('renderer');
$rform = $render->renderPlain($register_form);
echo $rform;
?>
</div> <!-- Register modal content end-->
</div><!-- Register modal-dialog end -->
</div><!-- Register modal end -->
<script>
var registerForm = document.getElementById('user-register-form');
if (registerForm ) {
// capture registration submit, prevent from submission and use ajax to get response
registerForm.onsubmit = async (e) => {
e.preventDefault();
// callRegCaptcha();
elms =document.getElementById("user-register-form").elements;
var url = "https://website/user/register"; //website is a true url
fetch(url, {
method: 'POST',
body: elms
})
.then(response=>response.json())
.then(json => {
console.log(json);
});
}
}
</script>
{message: 'Not acceptable format: json'}
message: "Not acceptable format: json"
[[Prototype]]: Object
Next, I have tried to follow this posts response by @Fons Vandamme of installing Rest UI and configuring the user registration
user registration with rest
message: "No user account data for registration received."
[[Prototype]]: Object