I am trying to change the message on the button click with ajax in laravel but for some reason, it is not working. I searched it online. Most of the insertion of jquery or ajax links is a problem. I think I’ve entered that correctly, I don’t know what else can be an issue.
There is also no issue in the console of the browser.
Scripts Links in my main layouts.app class
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
{{-- <script type="text/javascript" src="{{ asset('js/custom.js') }}"></script> --}}
<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
<!-- JQUERY -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
crossorigin="anonymous"></script>
<script src="https://markcell.github.io/jquery-tabledit/assets/js/tabledit.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-migrate-3.3.2.min.js"
integrity="sha256-Ap4KLoCf1rXb52q+i3p0k2vjBsmownyBTE1EqlRiMwA=" crossorigin="anonymous"></script>
<!-- Bootstrap js -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-VHvPCCyXqtD5DqJeNxl2dtTyhF78xXNXdkwX1CZeRusQfRKp+tA7hAShOK/B/fQ2" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous">
</script>
Code inside .blade.php
<div id='msg'>This message will be replaced using Ajax. Click the button to replace the message.</div>
<button id='btn' type="button" class="btn btn-secondary btn-rounded btn-sm my-0">
Replace Message
</button>
ajax script inside the same blade.
<script>
$(document).ready(
$("btn").on("click",
function() {
$.ajax({
type:'POST',
url:'/getmsg',
data:'csrf-token = <?php echo csrf_token() ?>',
success:function(data) {
dd(":1");
$("#msg").html(data.msg);
}
});
});
)
</script>
web.php linking
Route::post('/getmsg', [CustomerController::class, 'index']);
index function inside the CustomerController
public function index()
{
$msg = "This is a simple message.";
return response()->json(array('msg' => $msg), 200);
}