![]() |
Ajax post not working, can't figure this one out - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Ajax post not working, can't figure this one out (/showthread.php?tid=78645) |
Ajax post not working, can't figure this one out - superior - 02-19-2021 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? RE: Ajax post not working, can't figure this one out - InsiteFX - 02-19-2021 Try using the base url on the url PHP Code: url: "<?php base_url('ajax/newsletter';?>", RE: Ajax post not working, can't figure this one out - superior - 02-20-2021 (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... RE: Ajax post not working, can't figure this one out - InsiteFX - 02-20-2021 You may have just found a bug I would report it. RE: Ajax post not working, can't figure this one out - iRedds - 02-20-2021 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) RE: Ajax post not working, can't figure this one out - superior - 02-20-2021 (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();. RE: Ajax post not working, can't figure this one out - iRedds - 02-20-2021 (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. ![]() ![]() RE: Ajax post not working, can't figure this one out - superior - 02-20-2021 (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. |