0

I'm trying to use jQuery .post method in CodeIgniter; Even though I send request to the same domain and there should be no cross-domain issue, I get empty response. Here is my code:

$('#send-transaction').click(function(){
    $.post('/authorized/test1',function(data){alert(data);});
});

And function test1() of authorized controller looks like:

function test1() {echo "test";}

How can I get it to return the function's output (in this case, string "test")?

UPDATE

The reason is in invalid controller; Moving discussion to another question:

Codeigniter controller not returning requested function

Community
  • 1
  • 1
Vasily802
  • 1,703
  • 2
  • 18
  • 35

2 Answers2

1

Codeigniter includes protection from Cross Site Request Forgery (CSRF) attacks. This means any non-GET requests need to include a special token to tell the server that they're legitimate.

Here's an SO question that shows how to get that token without having to render a form on the page, and here's one with a bunch of answers showing how to integrate that with an ajax request.

Community
  • 1
  • 1
James Mason
  • 4,246
  • 1
  • 21
  • 26
  • I included the following lines of code to the view file from where I make jQuery .post request: $this->security->get_csrf_hash(); $this->security->get_csrf_token_name(); Still I get the same result - empty response. Do I need to somehow incorporate the token value into my post request? – Vasily802 May 09 '14 at 13:50
  • I included the code to the view file from where I make the request. Still I get the same result - empty response. Do I need to somehow incorporate the token value into the request? – Vasily802 May 09 '14 at 13:58
  • Yes, you need to include it in the request. I added a link to another SO question that shows a bunch of ways to do that. – James Mason May 09 '14 at 14:08
  • Ok, now my code looks like this: `var tokenname = 'security->get_csrf_token_name(); ?>'; var tokenvalue = 'security->get_csrf_hash(); ?>'; $.post('/authorized/test1',{tokenname:tokenvalue},function(data){ alert(data);})` Still getting the same result: request status 200 OK, response blank. – Vasily802 May 09 '14 at 14:52
0

Change PHP code as below:

function test1() {echo "test"; exit;}
cagatayodabasi
  • 762
  • 11
  • 34
Rakesh P.
  • 58
  • 7