ajax example with laravel and jquery

Solutions on MaxInterview for ajax example with laravel and jquery by the best coders in the world

showing results for - "ajax example with laravel and jquery"
Francesca
01 Sep 2018
1$(document).ready(function () {
2    const post_id = $("#post-detail").attr("data-post-id");
3    getCommentsOfPosts(post_id)
4
5    $("#comment-submit-form").submit(function(e){
6        e.preventDefault();
7        const formData     = $(this);
8        const submitButton = $("#comment-submit-button");
9        const post_id      = $("#post-id-comment").val();
10
11        submitButton.html('Saving....<i class="fa fa-spin fa-spinner" aria-hidden="true"></i>');
12
13        $.ajax({
14            method: "POST",
15            url: "/api/comments/" + post_id,
16            data: formData.serialize(),
17            success: (result) => {
18                submitButton.html('Save');
19                $("#comment-errors-data").html('');
20                $("#comment-input").val('');
21                $("#successMessage").removeClass('visually-hidden');
22                getCommentsOfPosts(post_id);
23            },
24            error: (error) => {
25                if(error.status === 422) { // "Unprocessable Entity" - Form data invalid
26                    $("#successMessage").addClass('visually-hidden');
27                    var message = error.responseJSON.errors ? error.responseJSON.errors.comment ?  error.responseJSON.errors.comment[0] : '' : '';
28                    $("#comment-errors-data").html(message);
29                    submitButton.html('Save');
30                }
31            }
32        });
33    });
34
35});
36
37
38/**
39* Get comments of a post
40*/
41function getCommentsOfPosts(post_id) {
42    if( typeof post_id !== 'undefined' && post_id !== '' && !isNaN(post_id)) {
43        $.ajax({
44            method: "GET",
45            url: "/api/comments/" + post_id,
46            success: (result) => {
47                $("#post-comments").html(result);
48            },
49            error: (error) => {
50                alert('Something went wrong to fetch datas...');
51            }
52        });
53    }
54}
queries leading to this page
jquery ajax method w3school in laraveljquery ajax example laravellaravel ajax examplejquery ajax laravel examplelaravel ajax callajax request in laraveljquery ajax method put laravel 8can we use ajax in laravelhow to write laravel code in ajax responseajax call jquery laravelajax jquery laravel document onajax code example in laravelshould i use ajax with laravelresponse ajax jquery laravel controllerjquery ajax with laraveljquery ajax laravelajax jquery in laravelhow to make ajax inside ajax laravelajax jquery in laravel projecthow to use ajax in laravel 8why ajax response full html page laravelusing ajax with laravelajax jquery laraveljquiry and ajax for laravelhow to handle ajax request in laravelhow to get ajax response in jquery in laravel applicationajax call in laravellaravel ajax tutorialjquery ajax method laravellaravel ajax requestuse ajax in laravelwhere to use ajax in laravel 8insert using ajax in laravelajax request syntax in laravellaravel ajax jquery formajax in laravellaravel 8 jquery and ajaxsend ajax request jquery laravelajax call laravelsend jquery ajax request laraveljquery ajax method in laravelajax example jquery laraveljquery ajax code for laravelajax insert jquery laravelset ajax call in laravellaravel how to do ajax callsview with javascript in laravel ajax responseajax request jquery in laravellaravel jquery ajaxlaravel jquery ajax data retrivejquery ajax in laravelajax url in laravel from jshow to make ajax call in component in laravelajax use in laravelajax example with laravel and jqueryhow to use ajax in laravelcreate ajax request in laravelajax laravel jqueryajax call in laravel by jqueyusing jquery ajax method in larav laravellaravel ajax and jquery pos systemajax example with laravel and jquery