Welcome Guest, Not a member yet? Register   Sign In
Getting 500 error with CI 2.0.x, ajax and CSRF enabled
#11

[eluser]Matalina[/eluser]
100% certain it works: Here is the ajax documentation on data

Quote:data Object, String
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
#12

[eluser]CroNiX[/eluser]
POST data is not sent as JSON. You have the choice to use a json object for the post values, or enter a manually generated query string (which is what serialize() does). A post string looks pretty much like a query string (separated by ampersands), just sent via a different method. jQuery takes your json object internally and translates it to a POST string when sending it out.

Check out the headers sent along with a form from any website using POST, even the one using jquery. It will look like that.
#13

[eluser]beaufrusetta[/eluser]
[quote author="Matalina" date="1339522097"]100% certain it works: Here is the ajax documentation on data

Quote:data Object, String
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
[/quote]

Badass! Then that helps out my jQuery quite a bit... Smile
#14

[eluser]CroNiX[/eluser]
You can also just use the jQ shortcut, $.post(), instead of $.ajax specifying the POST "type". It's shorter codewise.
Code:
$.post(
  "/client/saveOrder",       //url
  $('#form_id').serialize(), //data object
  function(data){            //callback function
    //callback
  }
);
#15

[eluser]jshultz[/eluser]
This isn't going to be a form, though. It's an UL with a list of LIs. I'm using the jquery plugin to allow me to be able to reorder the li's. When the li position is updated then it updates the db with the new ranking order for the pages. So, in short, there isn't any form data. Sad
#16

[eluser]CroNiX[/eluser]
jQueryUI sortable has it's own serialize method just for that Smile
http://jqueryui.com/demos/sortable/#method-serialize

In your sortable "change" event (or however you are choosing to save the sorted order), just do an ajax $.post() sending the sortable order as data using the serialize() method. Basically the same thing as above except sending the sortable serialized object instead of a serialized form.

Just append the csrf data to it when sending.
#17

[eluser]CroNiX[/eluser]
It would be something like
Code:
newOrder = $( "#reorder" ).sortable('serialize');
newOrder += '&csrf;_test_name=' + $.cookie('csrf_test_name') + ';';

Strange, the forum is adding a semicolon after &csrf; there...
#18

[eluser]jshultz[/eluser]
Got it! Thanks guys!

My code ended up looking like this:

I used both the token name and the name of the cookie to get it to work. I couldn't have done it without your help. Thanks! Smile

Code:
$('#reorder').sortable({
        opacity: '0.5',
        update: function(e, ui){
            newOrder = 'csrf_test_name=' + $.cookie('csrf_cookie_name') + '&';
            newOrder += $( "#reorder" ).sortable('serialize');
            console.log(newOrder);
            $.ajax({
                url: "/client/saveOrder",
                type: "POST",
                data: newOrder,
                csrf_test_name: $.cookie('csrf_cookie_name'),
                // complete: function(){},
                success: function(feedback){
                    console.log('success');
                    $("#feedback").html(feedback);
                    //$.jGrowl(feedback, { theme: 'success' });
                }
            });
        }
    });




Theme © iAndrew 2016 - Forum software by © MyBB