![]() |
JQuery post parameters do not get to my controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: JQuery post parameters do not get to my controller (/showthread.php?tid=61267) Pages:
1
2
|
JQuery post parameters do not get to my controller - talad - 04-06-2015 Hi All, I know this sounds like very similar issues already reported, but none of the solutions seems to work for me. The parameters passed from my JQuery post (or Ajax) do not get to my controller. I'm passing the csrf code, and also tried to disable it, but same issue. I am fairly new to codeigniter and think I am missing something basic, but I need some guidance. My controller is very simple: function __construct() { parent::__construct(); $this->load->helper('form'); $this->load->helper('html'); $this->load->model('users_model'); $this->load->library('form_validation'); parse_str($_SERVER['QUERY_STRING'],$_POST); } function create() { var_dump($_POST); $this->form_validation->set_rules('new_loc_loc_name', 'Address', 'required|urldecode'); if ($this->form_validation->run() == TRUE) { $this->users_model->filter_create(); } else { $this->load->view('filters/test'); } } And my view also very simple: <html> <head> <script src="/asset/js/external/jquery-1.11.2.js"></script> <link rel="stylesheet" href="/asset/css/external/1.11.3-jquery-ui.css" /> <script src="/asset/js/external/jquery-ui-1.11.3.js"></script> </head> <body> <?= form_open('user_filters/create', array('id' => 'form_id')); ?> <table class="main-dialog-table"> <tr> <td><label for="location">Location</label></td> <td> <input class="form_field_input" type="text" name="new_loc_loc_name" id="new_loc_loc_name"/> </td> </tr> <tr> <td> <input type="button" value="Create" id="new_loc_add" /> </td> </tr> </table> </form> </body> <script type="text/javascript"> $(document).ready(function(){ $('#new_loc_add').click( function() { var url = $("#form_id").attr('action'); $.post(url,$("form#form_id").serialize()); }); }); </script> </html> Looking at the Network tab (using chrome), I can see the post request finished successfully: Request URL:http://localhost/index.php/user_filters/create Request Method ![]() Status Code:200 OK And the parameters passed seems to be correct: csrf_test_name:af1ff53c0eef00ea08cc4407d978822c new_loc_loc_name:aa Further more, if I will submit the following from the browser, I can see the parameters going through: http://localhost/index.php/user_filters/create?name=aa Any help would be appreciated. Thanks in advance. RE: JQuery post parameters do not get to my controller - casa - 04-06-2015 Perhaps put in your config file $config['csrf_protection'] = FALSE; If its at TRUE, your are in codeigniter userguide a method with CSRF but i don't try it at this day. link : http://www.codeigniter.com/userguide3/libraries/security.html?highlight=csrf#CI_Security::get_csrf_token_name Then, you need to get your data in your controller : PHP Code: function create() RE: JQuery post parameters do not get to my controller - talad - 04-06-2015 Thanks a lot casa. I've tried setting csrf_protection to false, but same result. Since this->post is still available in the model, I am retrieving all data and do processing in the model itself. The problem is that the parameters don't even reach that stage. $_POST is always empty at the start of the method: array(0) { } What I also don't understand is that even with csrf protection on, if I manually go to http://localhost/index.php/user_filters/create?name=aa from a browser, the following goes through fine: array(1) { ["name"]=> string(2) "aa" } Shouldn't this be csrf protected? Thanks. (04-06-2015, 10:53 AM)casa Wrote: Perhaps put in your config file $config['csrf_protection'] = FALSE; RE: JQuery post parameters do not get to my controller - nekalv - 04-07-2015 I just try it and i cannot found nothing wrong with the code. Is your problem still going on? https://jsfiddle.net/h94v8qkq/ RE: JQuery post parameters do not get to my controller - talad - 04-08-2015 Thank nekalv. Yes, still have the problem. U also didn't think there could be something wrong with the code since it is pretty basic. My suspicion is something to do with my environment but I have no idea what to check. I'm pretty stuck there. (04-07-2015, 03:47 PM)nekalv Wrote: I just try it and i cannot found nothing wrong with the code. Is your problem still going on? RE: JQuery post parameters do not get to my controller - mwhitney - 04-08-2015 Have you checked in your browser's debugger to make sure jQuery is loading properly? Generally speaking, the relative paths to your js/css files in your view are likely to break on you. If you set $config['csrf_protection'] = FALSE; as casa recommended previously, then your controller is not going to use the CSRF protection. However, if you manually input the URL with a query string into a browser, you're not using a POST, anyway, so csrf_verify() will just set a CSRF cookie and move along. RE: JQuery post parameters do not get to my controller - talad - 04-11-2015 Thanks mwhitney. I've changed my jquery lib to load from google rather from locally: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> And removed all other local libraries. I've changed the click event to call directly to the controller using full path, such that: $('#new_loc_add').click( function() { var url = $("#form_id").attr('action'); $.post("http://localhost/index.php/user_filters/create?name=aa");//url,$("form#form_id").serialize()); }); }); And I've disabled the csrf in the config file: $config['csrf_protection'] = FALSE; When submitting, I can see the request been sent successfully, but the parameters still not reaching the controller. Request URL:http://localhost/index.php/user_filters/create?name=aa Request Method ![]() Status Code:200 OK If I'll copy paste the URL request to the browser, the parameters do get to the controller. RE: JQuery post parameters do not get to my controller - talad - 04-11-2015 So I've cleaned up the code to the bare minimum. Now my controller is: function test() { var_dump($_POST); } And my view is: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body> </body> <script type="text/javascript"> $(document).ready(function(){ $.post("http://localhost/index.php/user_filters/test?name=aa"); }); </script> </html> And csrf is disable. The parameters still does not reach the controller. Thanks. RE: JQuery post parameters do not get to my controller - CroNiX - 04-11-2015 You're mixing a query string (get) with post. Not sure if that will work but seems odd. How do you know it doesn't reach the controller. You don't do anything in the success event of the ajax call, like display what is returned. It just goes into a black hole. Just for giggles, try: PHP Code: $.post( RE: JQuery post parameters do not get to my controller - talad - 04-12-2015 Hi CroNiX, I've put the dump at the start of the controller and I expected to see the post variables there. I've put your code and what I get in the alert is an empty array: array(0) { } Thanks. |