Ajax post not working, can't figure this one out |
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) { Controller PHP Code: public function newsletter() And my routing has a $routes->post(), i'm i doing something wrong here?
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 )
(02-19-2021, 09:42 PM)InsiteFX Wrote: Try using the base url on the url 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...
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 )
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) (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. Thank you for the documentation page, i'm receiving an HTTP 500 (Internal Server.....). Request: ![]() Error: ![]() 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();. (02-20-2021, 09:15 AM)superior Wrote: EDIT: Congratulations. But why did you put the header then? Something I already forgot how this your jQuery works. ![]() ![]() (02-20-2021, 10:33 AM)iRedds Wrote:(02-20-2021, 09:15 AM)superior Wrote: EDIT: 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. |
Welcome Guest, Not a member yet? Register Sign In |