0

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;
    }   
}
1
  • Can you show your web.php content where you have defined the route? Commented Oct 15, 2019 at 11:53

3 Answers 3

0

I think that maybe your router not defined properly.

Can you show your router.php file ?

Sign up to request clarification or add additional context in comments.

1 Comment

I have used this in my route Route::post("/getEmpBalance","AjaxController@getEmployeeBalance"); now tell me whats the error
0

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

1 Comment

I am having localhost of this version as well and in the localhost version i did not get any type of error. and every thing is going very fine. but it gives error online only.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.