Welcome Guest, Not a member yet? Register   Sign In
Ajax post not working, can't figure this one out
#1

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
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: { emailaddremailaddr },
        
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($resp200);
    }

    // Cannot access this method
    return $this->failForbidden();


And my routing has a $routes->post(), i'm i doing something wrong here?
Reply


Messages In This Thread
Ajax post not working, can't figure this one out - by superior - 02-19-2021, 12:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB