![]() |
Cannot get AJAX to work for Post/Create - 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: Cannot get AJAX to work for Post/Create (/showthread.php?tid=82016) Pages:
1
2
|
Cannot get AJAX to work for Post/Create - spreaderman - 06-04-2022 This is my controller; PHP Code: public function task_create(){ and this is task_create.js Code: $(function() { And this is a part of my view Code: <form class="form-horizontal" action="<?= route_to('admin/tasks/create'); ?>" method="post" id="form-task-create"> Whenever i run it, I get always get a validation error. This is the console log; {success: false, msg: 'There are some validation errors'} msg: "There are some validation errors" success: false [[Prototype]]: Object The above appears whether I leave the input box blank or with a string. Any pointers appreciated. RE: Cannot get AJAX to work for Post/Create - spreaderman - 06-04-2022 I cannot seem to find the form data being submitted. It hits the right controller and method though. I should be able to access the form data like this right? $this->request->getVar("description"); or even getPost, right? Above is all null or blank. The method does see the ajax as a post when I do this; PHP Code: Any pointers appreciated. RE: Cannot get AJAX to work for Post/Create - iRedds - 06-04-2022 Check the browser console to see if data is being sent. RE: Cannot get AJAX to work for Post/Create - InsiteFX - 06-04-2022 Your ajax url is wrong try this one. Code: url: "<?= base_url('admin/tasks/create');?>", RE: Cannot get AJAX to work for Post/Create - iRedds - 06-05-2022 (06-04-2022, 11:59 PM)InsiteFX Wrote: Your ajax url is wrong try this one. url correct. '/admin/tasks/create' is a relative path and will use the domain of the loaded page. for page https://domain/some/page, '/admin/tasks/create' -> https://domain/admin/tasks/create RE: Cannot get AJAX to work for Post/Create - spreaderman - 06-05-2022 (06-05-2022, 12:18 AM)iRedds Wrote:(06-04-2022, 11:59 PM)InsiteFX Wrote: Your ajax url is wrong try this one. Thanks. Even if I put in the full path, like "https://www.example.com/admin/tasks/create" I get the same thing. If i log console.log(data); I see this; {success: false, msg: 'There are some validation errors'} But it give that back regardless of whether I pass validation or not I think. (06-05-2022, 02:49 AM)spreaderman Wrote:(06-05-2022, 12:18 AM)iRedds Wrote:(06-04-2022, 11:59 PM)InsiteFX Wrote: Your ajax url is wrong try this one. And I cannot seem to catch the form data. RE: Cannot get AJAX to work for Post/Create - iRedds - 06-05-2022 Speaking of the browser console, I meant the network section. There you can see each request made by the browser (request and response headers, sent data and response body) If the data is a send, then $_POST or request->getPost() must contain the data. Perhaps some filter replaces the request class. RE: Cannot get AJAX to work for Post/Create - spreaderman - 06-05-2022 (06-05-2022, 03:05 AM)iRedds Wrote: Speaking of the browser console, I meant the network section. I checked the NETWORK tab and under PAYLOAD it shows; csrf_token_mamamia=[token-snip]&description=canada under headers, I see; Request Method: POST Status Code: 200 OK under response I see; {"success":false,"msg":"There are some validation errors"} RE: Cannot get AJAX to work for Post/Create - InsiteFX - 06-05-2022 (06-05-2022, 12:18 AM)iRedds Wrote:(06-04-2022, 11:59 PM)InsiteFX Wrote: Your ajax url is wrong try this one. Thanks, I always used the base_url and never had a problem with ajax. but this is good to know. RE: Cannot get AJAX to work for Post/Create - spreaderman - 06-06-2022 BTW, is this what it is supposed to look like in the NETWORK tab of chrome? Is it sending correctly? csrf_token_mamamia=[token-snip]&description=canada |