Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use ajax with slim csrf #65

Closed
arbims opened this issue Nov 28, 2016 · 5 comments
Closed

How to use ajax with slim csrf #65

arbims opened this issue Nov 28, 2016 · 5 comments

Comments

@arbims
Copy link

arbims commented Nov 28, 2016

alway when sublit data en post in ajax i get this response requests Failed csrf token

@mrdus
Copy link

mrdus commented Nov 29, 2016

Hello,
I had the same problem.
I solved it :
You need to get the csrf_token values in js.
So i added id to my inputs :
#php

$namekey = $csrf->getTokenNameKey();
$valuekey = $csrf->getTokenValueKey();
$name = $request->getAttribute($namekey);
$value = $request->getAttribute($valuekey);

<input id="csrf_name" type="hidden" name="$namekey " value="$name">

<input id="csrf_value" type="hidden" name="$valuekey"  value="$value">

#javascript

var csrfName = document.getElementById('csrf_name').value;
var csrfValue = document.getElementById('csrf_value').value;

var csrfData = {"csrf_name" : csrfName,"csrf_value" : csrfValue}; 
$.ajax({
    url : "AJAX_POST_URL",
    type: "POST",
    data : csrfData,
    success: function(data, textStatus, jqXHR)
    {
        //data - response from server
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
 
    }
});

@arbims
Copy link
Author

arbims commented Nov 30, 2016

thnx 👍 good work

@schnittstabil
Copy link
Contributor

Please be aware of the CSFR tokens for multiple forms\post per page (Ajax) #59 issue.

@mridah
Copy link

mridah commented Jul 24, 2018

Any idea how to use Persistant Tokens ??? I don't understand where and how to add a 6th parameter ?

@akrabat
Copy link
Member

akrabat commented Jul 25, 2018

If you are using the example in the README:

$container['csrf'] = function ($c) {
    $csrf = new \Slim\Csrf\Guard();
    $csrf-> setPersistentTokenMode(true);
    return $csrf;
};

@akrabat akrabat closed this as completed Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants