Welcome Guest, Not a member yet? Register   Sign In
How to Insert into database with Ajax
#1

[eluser]titolancreo[/eluser]
Hi!
I have a calendar and I want to insert some dates by ajax. I don't know what i'm doing wrong but it never goes to my controller...

My ajax call in view:
Code:
var postData = {
    'diaCom': startDay,
    'mesCom':startMonth,
    'anioCom':startYear,
    'horaCom':startHour,
    'minCom':startMin,
    'diaFin': endDay,
    'mesFin':endMonth,
    'anioFin':endYear,
    'horaFin':endHour,
    'minFin':endMin,
    submit:true
};
$.ajax({  
                type: "POST",  
                url: "<?php echo base_url('disponibilidad'); ?>",  
                dataType: "json",
                data: postData,
                success: function()
                {
                        alert("OK");
                },
                error: function()
                {
                    alert("Error");
                }
                
            });

If I write localhost/PFC/disponibilidad, I go to my controller, so that works, but If I call that function, I always see the error alert.

Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
In the rendered HTML source, what's the value of 'url'?

Is it possible that you've haven't prefixed $config['base_url'] with http:// in ./application/config/config.php?

Also, if you're using Firefox, the Firebug extension can help a lot. You can monitor network traffic and see where requests are going, and what's being returned.
#3

[eluser]titolancreo[/eluser]
The source code its Ok, I can see h ttp : / / localhost/PFC/disponibilidad

And using firebug, I can see it tries to go there, but it returns Internal Server error 500 (The action you have requested is not allowed)


OK, I found the problem.

I have $config['csrf_protection'] = TRUE; and if I turn it to FALSE, it works...

how can I send it by ajax with csrf_protection=TRUE??
#4

[eluser]Otemu[/eluser]
The post here and comments on it should help you solve your issue

http://www.web-and-development.com/5-rea...deigniter/
#5

[eluser]titolancreo[/eluser]
[quote author="Otemu" date="1364232954"]The post here and comments on it should help you solve your issue

http://www.web-and-development.com/5-rea...deigniter/[/quote]

I changed the field because I read that, but it's supposed to be a way to protect the web against csrf, because if not, I can send you an email with a link and when you click it, maybe, I'm changing your profile, or something worse...
#6

[eluser]LuckyFella73[/eluser]
Code:
OK, I found the problem.

I have $config[‘csrf_protection’] = TRUE; and if I turn it to FALSE, it works…

how can I send it by ajax with csrf_protection=TRUE??

you have to read the value of the hidden csrf field generated automatically and
send it along the other post values.

For example you have this in your config.php:
Code:
$config['csrf_token_name'] = 'csrf_field_name';

Code:
// javascript part:
var csrf_field_value : $("input[name=csrf_field_name]").val();

var postData = {
    'csrf_field_name': csrf_field_value,
    'diaCom': startDay,
    'mesCom':startMonth,
    'anioCom':startYear,
    'horaCom':startHour,
    'minCom':startMin,
    'diaFin': endDay,
    'mesFin':endMonth,
    'anioFin':endYear,
    'horaFin':endHour,
    'minFin':endMin,
    submit:true
};




Theme © iAndrew 2016 - Forum software by © MyBB