简体   繁体   中英

Laravel 5.0 TokenMismatchException with uploadify on firefox

Hello i'm developing in Laravel 5.0

Currently i will update a image file on my server in js. For this i use the plugin uploadify and it works on google chrome.

But on mozilla firefox i get an exception TokenMismatchException.

I debug my middleware (VerifyCsrfToken) and i find

$token = chbeVVpy5oyE3p1IDe4TzHFNJwEzZ7I6xnk9d03R

and

$request->session()->token() = wW2S9zf1Xo82DxdmjXj0j5zubSWKhvadeRG20cmv

that's the reason of my exception. But i don't understand why the both values isn't equals. My js call is

<script type="text/javascript">         
    <?php $timestamp = time();?>        
    $(function() {          
        $('#file_upload').uploadify({
                    'formData'     : {
                        'method' : 'post',
                        'timestamp' : '{!! $timestamp !!}',
                        '_token'     : '{!! csrf_token() !!}'
                    },
                    'swf'      : '{!!asset("js/uploadify/uploadify.swf")!!}',
                    'uploader' : '{!! asset("uploadify")!!}',
                    'buttonText' : 'Votre logo'             });         });     </script>

This code works on google chrome ...

Edit : the plugin uploadify uses flash

Try to use this. JQuery will set token header in ajax request header everytime so that you don't need to setup yourself.

// setting laravel 5 xsrf-token
jQuery.ajaxSetup({
    headers: {
        'X-XSRF-TOKEN': cookie('XSRF-TOKEN')
    }
});

And here is my javascript cookie function.

/**
 * get the cookie
 * @param {string} name
 * @returns {string}
 */
function cookie(name) {
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : decodeURIComponent(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM