0
votes

I have used this in my html part:

<meta name="csrf-token" content="{{ csrf_token() }}" />

In script I have done:

$(document).ready(function() {
    $(document).on('change','#user_id', function() {
        try {
            var user_id = $(this).val();
            $('#balance_amount').val("Loading response...");
            $.ajaxSetup({
                beforeSend: function(xhr, type) {
                    if (!type.crossDomain) {
                        xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
                    }
                },
            });
            $.ajax({
                url: "{{ URL::to('/getEmpBalance')}}",
                method: 'post',
                data: {
                    'user_id' : user_id
                },
                success: function(result) {
                    console.log("Success "+result);
                    $('#balance_amount').val(result);
                },
                error: function(e, jqXHR, textStatus, errorThrown) {
                    // console.log("Error "+e);
                    // console.log(JSON.stringify(jqXHR));
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                    $('#transaction_description_editor').html(e);
                }
            });
            //console.log(user_id);
        } catch (e) {
            // console.log(e);
            // console.log(warning);
            // console.log(text);
        }
    });
});

But after sending ajax post request it gives error and when I checked, I found

{message: "Method App\Http\Controllers\AjaxController::getEmployeeBalance does not exist.",…} exception: "BadMethodCallException" file: "/home/studyn5/exp.studynextglobal.com/vendor/laravel/framework/src/Illuminate/Routing/Controller.php" line: 68 message: "Method App\Http\Controllers\AjaxController::getEmployeeBalance does not exist." trace: [{function: "__call", class: "Illuminate\Routing\Controller", type: "->"}, {,…}, {,…}, {,…}, {,…},…]

But it my ajax Controller I have mentioned this method

public function getEmployeeBalance(Request $request) {
    $user_id = $request->user_id;
    $exp_amount = DB::table('exp_amounts')
                        ->where('user_id', $user_id)
                        ->orderBy('created_at','desc')
                        ->first();

    if ($exp_amount === null) {
        echo "0";
    }
    else {
        $balance_amount = $exp_amount ->total_amount;
        echo $balance_amount;
    }   
}
3
Can you show your web.php content where you have defined the route? - Rehan

3 Answers

0
votes

I think that maybe your router not defined properly.

Can you show your router.php file ?

0
votes

I think the problem is in the echo functional You are trying to echo object, You can convert it to json and after it echo Your variable This will throw error in a future

0
votes

I'd make sure everything to do with naming is right (even down to capitalisation). If you're saying nothing's changed, make sure your controller and class name are exactly the same.

You're saying it works fine on local and not in an online version. The only time i've experienced similar is if my production OS and my local OS are different. Different OS' process naming styles and certain characters differently.

It would be very helpful to know the differences between the two systems and also to see everything to do with any files involved e.g. viewing a screenshot of your IDE to see your file name and then your class name. So if you could post the following it would be beneficial to figuring this out.

  • Script file with the ajax function
  • Screenshot of the web.php routes with the suspect route in view
  • The controller, right from the opening PHP tags
  • A screenshot of the app directory where the route to the AjaxController is expanded.