CodeIgniter Forums
AJAX post returns 500 error - 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: AJAX post returns 500 error (/showthread.php?tid=1203)



AJAX post returns 500 error - Kenneth_H - 02-18-2015

Hi
I have tried to build an installer for my codeigniter application and after completing af small form for collecting the database information it should execute a method for setting everything up.
This function is located in the install controller and the method is called runci and takes no arguments.
Currently looks like this:
PHP Code:
$data = array(
                
'status' => 'OK',
                
'http_response' => 200,
                
'message' => 'JSON data recieved'
            
);
            return 
json_encode($data); 
My JS looks like this and is written in-line at the bottom of my view-file:
Code:
//AJAX call to submit and install
            $(document).ready(function(){
                var formdata = <?php print json_encode($_POST); ?>;
                console.log(formdata);
                try {
                    $.post('<?php print site_url(); ?>install/runci', formdata);
                }
                catch(e) {
                    console.log(e);
                }
            });
HTML output is correct, but after outputting the formdata to the console and then posting, I get this error back in the console:
Code:
POST http://ignitercms.khit.dev/install/runci 500 (Internal Server Error)
I know that something is wrong, but I cannot see what.

Only change from default CI 2.2.1 is that I have added wiredesignz HMVC Modular Extentions


RE: AJAX post returns 500 error - CroNiX - 02-18-2015

It could be because you are returning your json instead of echoing it out back to the browser during the ajax request?


RE: AJAX post returns 500 error - Kenneth_H - 02-18-2015

I have tried changing from return to echo and even to print, but still gives me same error in the console and nothing in CI log or php error log.


RE: AJAX post returns 500 error - Kenneth_H - 02-21-2015

I fixed the issue. Seems like it could not handle it via post, so changed it to run using get requests.
Code:
//AJAX call to submit and install
$(document).ready(function(){
     var formdata = <?php print json_encode($this->input->post(NULL, TRUE)); ?>;
     console.log(formdata);
     $.ajax({
         type: "GET",
         url: "<?php print site_url('install/runci'); ?>",
         data: formdata,
         success: function(msg) {
             console.log(msg);
         }
     });
});