3

I am new with laravel, I am having an issue when posting my data to controller in laravel while using ajax. My view contains a select box which is populated from database when a user select a value from select box i am trying to call ajax which will return details of salary of the user.The issue is it gives me 404 not found error.

The Code for the controller file is shown below.

    this function is defined inside a controller

    public function getPostSalary()
        {
            echo "ajax called";
            return 'true';
        }

The Routes file is

Route::post('company/salary-user', 'CompanyController@getPostSalary');

this is my ajax code from where the controller is being calleD

<script>
    $(document).ready(function() {
        $('#employee').change(function () {
            var values = $('#employee').val();
            if (values == '') {
                $.pnotify({
                    title: 'Message',
                    text: 'Please Select a User.',
                    type: 'error',
                    delay: 3000
                });
                return false;
            }
            var csrf = $('#csrf').val();
            $.ajax({
                url: '{!! URL::to("company/salary-user")!!}',
                type: "POST",
                data: { user_id: values, _token: csrf },
                cache: false,
                beforeSend: function () {

                },
                success: function (data) {
                    console.log(data);
                    $('#loader').hide();
                }
            });
        }); 
    });
</script>

Can someone help me to figure out what is the issue and what should be done to call my function.

6
  • your ajax file is in *.js ? or inside your blade template ? Commented Mar 20, 2017 at 12:05
  • and in your network tracking, whats is the url you are pointed to when event employee change happens ? Commented Mar 20, 2017 at 12:12
  • my js file is in blade tempelate and the url it showing is Commented Mar 20, 2017 at 12:23
  • "localhost/attendance/public/company/salary-user". Commented Mar 20, 2017 at 12:23
  • checkout my answer Commented Mar 20, 2017 at 12:24

3 Answers 3

2

Single quotes is for string so your url is not generating as you expecting. Use like this

  url : "{!! URL::to('company/salary-user')!!}",
Sign up to request clarification or add additional context in comments.

Comments

0

You can use direct url like

 $.ajax({
            url:'company/salary-user',
            type: "POST",
            data: { user_id: values, _token: csrf },
            cache: false,
            beforeSend: function () {

            },
            success: function (data) {
                console.log(data);
                $('#loader').hide();
            }
        });

Comments

0

replace this line:

{!! URL::to("company/salary-user")!!}

by :

{{ url('/company/salary-user') }}

or

{{ route('/company/salary-user') }}

13 Comments

it is giving me the same issue with that
LOL ,omg i was so dump, use either {{ url('/company/salary-user') }} or {{ route('/company/salary-user') }}
the issue is at it place
what's the actual url now you are pointed to ? and don't forget to clear your browser cache
and when using url('/company/salary-user') ?
|

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.