02-19-2021, 12:24 PM
Hello,
I'm working on a AJAX call, this is sending a POST to my controller. But this controller doesn't seem to get any value in getPost() and getGet().
JavaScript
Controller
And my routing has a $routes->post(), i'm i doing something wrong here?
I'm working on a AJAX call, this is sending a POST to my controller. But this controller doesn't seem to get any value in getPost() and getGet().
JavaScript
PHP Code:
$('#newsletterSignup').on('click', function(e) {
var emailaddr = $('#newsletterEmail').val();
var csrfToken = document.querySelector('meta[name="x-csrf-token"]').getAttribute('content');
// Post
$.ajax({
url: '/ajax/newsletter',
method: 'post',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
},
dataType: 'json',
data: { emailaddr: emailaddr },
success: function(resp) {
console.log(emailaddr);
console.log(resp);
document.querySelector('meta[name="x-csrf-token"]').setAttribute('content', resp.csrfHash);
}
});
});
Controller
PHP Code:
public function newsletter()
{
if( $this->request->isAjax() )
{
$email = $this->request->getPost('emailaddr');
$resp = [];
$resp['emailaddr']= (string)$email;
$resp['csrfHash'] = (string)csrf_hash();
return $this->respond($resp, 200);
}
// Cannot access this method
return $this->failForbidden();
}
And my routing has a $routes->post(), i'm i doing something wrong here?