0

i am try to get data using ajax in codeigniter but when i send request for get data to my controller at that time i get this error

The action you have requested is not allowed.

this is my ajax code

$.ajax({
     type:'POST',
    dataType: 'json',
    url: "redeem_drink/testjs",
    success: function(data){
        console.log(data);          
    },
    error: function(data){
        console.log(data);
    }
});

please help me how to setup ajax code for get and post data

Trushar Narodia
  • 3,511
  • 2
  • 11
  • 17

1 Answers1

0

The error is due to the CSRF value you have for the current submission. Which can be used to submit the form only once unless the value is updated by getting a new CSRF value as a response of the last form submit.

Make sure you update the CSRF value or Reload the page which will automatically refresh the CSRF value.

In your php code create a new CSRF value using the code.

$csrf = array(
        'name' => $this->security->get_csrf_token_name(),
        'hash' => $this->security->get_csrf_hash()
);

return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode($csrf));

and send the $csrf data as a response of last ajax submission of form.

Now update the CSRF of the all the form using javascript.

You can do that like this, Inside the success function.

$('form').find('input[name='+data.name+']').val(data.hash);
Nadir Laskar
  • 4,012
  • 2
  • 16
  • 33