简体   繁体   中英

ajax works locally fine but not on server laravel 8

I'm beginner on ajax with laravel. I want to post update with ajax. I implement ajax that works fine on local machine but when i upload on server it not working. I'm tired to solve this problem.

Hearder:

<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

body:

<script type='text/javascript'>
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');                           

$(document).on("click", ".update42" , function() {
var edit_id = 42;
var name = 1 + 1;
    $.ajax({
    url: '/dashboard/post/42',
    type: 'patch',
    data: {_token: CSRF_TOKEN,id: edit_id,download_count: name},
    success: function(response){
        alert(response);
    }
                                                });                                          
}); 
</script>

Just full address then solve

url: 'https:/yoursite.com/dashboard/post/42',

Add this to your script

    var WebURL = {!! json_encode(url('/')) !!}

then use it in your AJAX url

url: WebURL + '/dashboard/post/42',

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