简体   繁体   English

在laravel中使用ajax提交表单时找不到404

[英]getting 404 not found when submitting form using ajax in laravel

I am new with laravel, I am having an issue when posting my data to controller in laravel while using ajax. 我是laravel的新手,在使用ajax时将我的数据发送到laravel中的控制器时遇到问题。 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. 我的视图包含一个选择框,当用户从选择框中选择一个值时,它会从数据库填充我试图调用ajax,它将返回用户工资的详细信息。问题是它给我404找不到错误。

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 Routes文件是

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

this is my ajax code from where the controller is being calleD 这是我的ajax代码,来自控制器的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. 有人可以帮我弄清楚问题是什么,以及应该怎么做来调用我的功能。

Single quotes is for string so your url is not generating as you expecting. 单引号用于字符串,因此您的网址不会按预期生成。 Use like this 像这样使用

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

replace this line: 替换这一行:

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

by : 通过:

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

or 要么

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

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();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM