Using AJAX to send data to Coder Igniter controller function results in a 500 Internal Server Error

StackOverflow https://stackoverflow.com/questions/17710632

Domanda

I am trying to use AJAX to send data from a Code Igniter view to a controller that will handle the data as needed. I'm gathering the data using a JQuery plugin (Handsontable) and when the user hits the "save" button it extracts the required data from the table and executes the ajax function.

$.ajax({
    url: "/survey/save",
    data: {"data": data},
    type: "POST",
});

I am able to send it to a regular .php file which collects the data with $_POST but not my controller.

public function save() {
    $data = $this->input->post('data');

    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");    

    for ($i = 0, $size = count($data); $i < $size; ++$i) {
        fwrite($fh, $data[$i][0]."\t".$data[$i][1]."\t".$data[$i][2]."\n");
    }
    fclose($fh);
}

The above code is not what I really want the controller to do but if can successfully execute this code, I will be able to do what I wish.

I have a feeling it has something to do with the URL of the ajax function but I am extremely new to all of these languages and am probably overlooking something simple. Please let me know if I should include any other code!

È stato utile?

Soluzione 2

Answering my own question in case it helps others. The problem was my csrf settings. I realized turning off csrf protection fixed the problem but I didn't want to keep csrf protection off. I think CI may have came out with a whitelist to fix this but I just edited my config file as follows:

if(stripos($_SERVER["REQUEST_URI"],'/survey') === FALSE)
{
    $config['csrf_protection'] = TRUE;
}
else
{
    $config['csrf_protection'] = FALSE;
} 

Altri suggerimenti

Hey you have to change in ajax url The format of url must be absolute path for e.g.

In your view the ajax should be like this

$.ajax({
        url:'<?php echo site_url('survey/save'); ?>',
        type: 'post',
        data:  "data="+data,
        beforeSend:function(){
                //before send code for e.g. put a loader
        },
        success:function(result){ 
            // success result code goes here
        },
        error:function(jqXHR, status, error){
            if(status!='error')
                alert(error);   
        }
    });

Now in your controller you can get the data

$data=$this->input->post('data');

and one more thing you have to use relative path when your using fopen for e.g.

//you have used

$myFile = "testFile.txt";

//instead of that you have to use

$myFile="./YOUR_FOLDER_NAME/YOUR_FILE_NAME";

and you can set in your config.php

$config['base_url'] = '';

If you have done your config file in the CI Application/Config folder

$config['base_url'] = 'http://yourwebsite.com/';

then for your url it is

"<?php echo base_url();?>survey/save", 

The very simple and easy way try this code:

first you must have set your base_url in application/confiq.php or with .htaccess

your script:

<script type='text/javascript'>
var base_url = '<?=base_url()?>';

function m_ajax()
{
   var ids = $("#all_users").val();

   $.ajax({
      type:"POST",
      url: base_url+"history/home/get_users",
      data: "userid=" + ids,
      success: function(result){
        $("#m_ajax").html(result);
    }
    });

}
</script>

in your controller you can get the value of userid as post

 $userid = $this->input->post('userid');

and you can perform other operations according.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top