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
#2

Try using the base url on the url

PHP Code:
url"<?php base_url('ajax/newsletter';?>"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-19-2021, 09:42 PM)InsiteFX Wrote: Try using the base url on the url

PHP Code:
url"<?php base_url('ajax/newsletter';?>"

That doesn't work either, have tried this before asking to be shure. The weird thing is did recieve an answer in json, but the post variables aren't getting through to the method. Even the X-CSRF-TOKEN is validated in this request...
Reply
#4

You may have just found a bug I would report it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 02-20-2021, 09:05 AM by iRedds.)

If you are sending JSON then the getGet/getPost/getGetPost/getPostGet methods will not work as they access the $_GET and $_POST arrays.
Use getJSON, getJsonVar or getVar methods to get data

Retrieving Input (scroll to "Getting JSON data" section)
Reply
#6

(This post was last modified: 02-20-2021, 09:27 AM by superior.)

(02-20-2021, 09:04 AM)iRedds Wrote: If you are sending JSON then the getGet/getPost/getGetPost/getPostGet methods will not work as they access the $_GET and $_POST arrays.
Use getJSON, getJsonVar or getVar methods to get data

Retrieving Input (scroll to "Getting JSON data" section)

Thank you for the documentation page, i'm receiving an HTTP 500 (Internal Server.....).

Request:
[Image: Request.png]

Error:

[Image: HTTPError.png]

Not a single one of your sugestions seem to work, i don't think it's a bug in CI4 otherwise someone else would have spotted way before i did?


EDIT:
And just like that i've found the problem, the header send in the Ajax request 'Content-Type: application/json' caused my issue. The minute i removed that from the headers array i'm receiving the post with $request->getPost();.
Reply
#7

(02-20-2021, 09:15 AM)superior Wrote: EDIT:
And just like that i've found the problem, the header send in the Ajax request 'Content-Type: application/json' caused my issue. The minute i removed that from the headers array i'm receiving the post with $request->getPost();.

Congratulations. But why did you put the header then?

Something I already forgot how this your jQuery works.  Big Grin

Exclamation $_POST An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
Reply
#8

(02-20-2021, 10:33 AM)iRedds Wrote:
(02-20-2021, 09:15 AM)superior Wrote: EDIT:
And just like that i've found the problem, the header send in the Ajax request 'Content-Type: application/json' caused my issue. The minute i removed that from the headers array i'm receiving the post with $request->getPost();.

Congratulations. But why did you put the header then?

Something I already forgot how this your jQuery works.  Big Grin

Exclamation $_POST An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.


Haha thnx!
To be honest, no clue why this was put there. Never did this before so there shouldn't be a reason for it now.. Maybe because i've been looking for the X-CSRF-TOKEN header but i never used it before on projects with an Ajax request.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB