Welcome Guest, Not a member yet? Register   Sign In
ajax, jquery problem
#1

[eluser]seegan[/eluser]
Hi friends and brothers,
plz help me...

when i use like below its working fine (without passing data)..

Code:
<div id='result_table'>
</div>
.....script type='text/javascript' language='javascript'....
$('#getdata').click(function(){
    $.ajax({
            url: '&lt;?php base_url() ?&gt;message/sam',
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                    $('#result_table').append(output_string);
                } // End of success function of ajax form
            }); // End of ajax call
});
....script....

controller function
------------------------
Code:
{
        $data = 'success';
        echo json_encode($data);
    }

---------------------------------------------------------------------------------------


But when i use like below its not working.. (passing with data)

Code:
<div id='result_table'>
</div>
.....script type='text/javascript' language='javascript'....
$('#getdata').click(function(){
    var name= "johns";
    $.ajax({
            url: '&lt;?php base_url() ?&gt;message/sam',
            type:'POST',
            dataType: 'json',
            data: {  name: name },
            success: function(output_string){
                    $('#result_table').append(output_string);
                } // End of success function of ajax form
            }); // End of ajax call
});
....script....

controller function
------------------------
Code:
{
        $name = $this->input->post('name');
        echo json_encode($name);
    }

also i tried wit some other methods but did'nt got the result..
friends plz i need your help..
#2

[eluser]seegan[/eluser]
No 1 is here....?
#3

[eluser]PhilTem[/eluser]
I'd suggest changing the name of variable "name" to something like "varName" so that it won't interfere with the object attribute name.
#4

[eluser]seegan[/eluser]
ohh man cant get the result plz some one help me...
#5

[eluser]InsiteFX[/eluser]
Try
Code:
url: '&lt;?php base_url(); ?&gt;index.php/message/sam',

Or the way I do it, replace the $ in script tags with s
Code:
&lt;head&gt;
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

url: base_url+'index.php/message/sam',
#6

[eluser]seegan[/eluser]
Its not the problem..

problem is
Code:
If i set csrf protection = true
then this wont work

If i set csrf protection=false
then this works fine

Code:
If i use get then working fine for both csrf enable and disable

I need when csrf protection = true with post action then this code also need to work

any possible answers??
#7

[eluser]InsiteFX[/eluser]
You should have stated that in the beginning.

Read this
#8

[eluser]GrahamDj28[/eluser]
When using csrf protection you must pass the csrf token along with the rest of your post data.

To get the csrf token in your ajax you can add this function as a global function to your JS

Code:
function getCookie(cookie_name) {
    var i,x,y,ARRcookies=[removed].split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==cookie_name) {
            return unescape(y);
        }
    }
}

Just call the function with the name of your csrf cookie to get the token.
#9

[eluser]seegan[/eluser]
[quote author="GrahamDj28" date="1356872055"]When using csrf protection you must pass the csrf token along with the rest of your post data.

To get the csrf token in your ajax you can add this function as a global function to your JS

Code:
function getCookie(cookie_name) {
    var i,x,y,ARRcookies=[removed].split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==cookie_name) {
            return unescape(y);
        }
    }
}

Just call the function with the name of your csrf cookie to get the token.[/quote]

cant understand will you please explain in deep.??
#10

[eluser]InsiteFX[/eluser]
Read This:

CodeIgniter CSRF Protection With Ajax




Theme © iAndrew 2016 - Forum software by © MyBB