Laravel框架中不正确的Ajax url到控制器功能

问题描述 投票:0回答:1

我想要一个正确的ajax URL,该URL无法正常工作。我在控制台中得到这个:

获取XHR本地主机:8000 / Controller / getUnitSellingPrice [HTTP / 1.0 404找不到203ms]

create.blade视图

C:\Apache24\htdocs\printshopsales\resources\views\sales\create.blade.php

Controller

C:\Apache24\htdocs\printshopsales\app\Http\Controllers\SalesController.php

我尝试过这里的内容:

Ajax call Into MVC Controller- Url Issue

    <script>
        $(document).ready(function() {
            $("#stock_name").on('change', function () {
                let element = $(this);
                /*var MyAppUrlSettings = {
                    MyUsefulUrl : '/getUnitSellingPrice'
                }*/
                $.ajax({
                    //url: MyAppUrlSettings.MyUsefulUrl,
                    url: '/Controller/getUnitSellingPrice',
                    method: 'GET',
                    data: {
                        'stock_name' : element.val(),
                    },
                    success: function (response) {
                        $("#unit_selling_price").val(response.data).trigger('change');
                        console.log(response.data);
                    },
                });
            });
        });
    </script>

enter image description here

javascript php ajax php-7 laravel-5.8
1个回答
0
投票

您应为此方法添加一条路线。就像您的SalesController.php

在SalesController文件中:

public function getUnitSellingPrice()
{
    /* your code */
}

在路由文件中web.php

Route::any('sales/getunitsellingprice','SalesController@getUnitSellingPrice');

[更新您的jquery URL,例如:

url: '/sales/getunitsellingprice',

感谢

© www.soinside.com 2019 - 2024. All rights reserved.