skip to Main Content

I am trying to build a "view details" page for many "request" in database, with a customised progress bar, which can be access by calling RequestListController@show. I store progress for each "request" in MySQL. I want to get the progress of a request based on the id with Ajax so that I can use Jquery to update the progress bar each time the user open a "view details" page. But this doesn’t work, I use alert() to test if it works, and nothing will happen. If I remove the {{ $catRequest-id }}, I can get a alert. Does it mean I cannot use blade in the url in javascript? If so, is there a better way to get data in the database?

<script>
    $(document).ready(function () {
        $.ajax({
            url: "/ajax-get-progress/{{ $catRequest->id }}",
            type: "get",
            dataType: "json",
            success: function (response) {
                alert(response["progress"]);
            },
        });
    });
</script>
Route::get('/ajax-get-progress/{$catRequest->id}', 'AjaxController@getProgress'); // ajax gets progress by id

Route::get('/request-list/show/requestID/{requestID}', 'RequestListController@show');

public function getProgress($requestID)
{
    $progress = CatRequest::where('id', $requestID)->first();
    $userData['progress'] = $progress;
    echo json_encode($userData);
    exit;
}

2

Answers


  1. Chosen as BEST ANSWER
    Route::get('/ajax-get-progress/{requestID}', 'AjaxController@getProgress'); // ajax gets progress by id
    
    public function getProgress($requestID)
        {
            $progress = CatRequest::where('id', 1)->first()->progress;
            $userData['progress'] = $requestID;
            echo json_encode($userData);
            exit;
        }
    
    <script>
            $(document).ready(function(){
                $.ajax({
                            url: "/ajax-get-progress/{{ $catRequest->id }}",
                            type: "get",
                            dataType: "json",
                            success: function(response) {
                                alert(response['progress']);
                            }
                        });
            });
        </script>
    

  2. There are a lot of solutions on stackoverflow. That should do the trick:

    https://stackoverflow.com/a/58179530/7807619

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search