1

I want a correct ajax URL this one is not working. I am getting this in the console:

GET XHR localhost:8000/Controller/getUnitSellingPrice [HTTP/1.0 404 Not Found 203ms]

create.blade View

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

Controller

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

I have tried what is here:

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

Charleskimani
  • 440
  • 7
  • 25

1 Answers1

2

You should add a route to web.php file. Like in your SalesController.php

In SalesController file:

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

In Route file web.php

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

Update your jquery URL like:

url: '/sales-price/getunitsellingprice',

Thanks

Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24
  • @Charleskimani Try to run `http://localhost:8000/sales/getunitsellingprice` browser new tab. Check json data return or not. – Amit Senjaliya Oct 02 '19 at 07:03
  • @Charleskimani That is a problem. Laravel resource method understands `show` Action. So now I have updated my answer. Change route name. – Amit Senjaliya Oct 02 '19 at 07:16
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200286/discussion-between-amit-senjaliya-and-charleskimani). – Amit Senjaliya Oct 02 '19 at 07:18