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

10
  • Your Controller name is controller? Commented Oct 2, 2019 at 5:37
  • Change the line from ` url: '/Controller/getUnitSellingPrice',` to ` url: '/sales/getUnitSellingPrice',` Commented Oct 2, 2019 at 5:44
  • @Charleskimani What is a route define in web.php file? Commented Oct 2, 2019 at 5:47
  • 2
    It's quiet impossible for us to know what the correct URL is since we can't see your routes. Commented Oct 2, 2019 at 5:48
  • This is now a completely different question. Commented Oct 2, 2019 at 5:50

1 Answer 1

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

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

3 Comments

@Charleskimani Try to run http://localhost:8000/sales/getunitsellingprice browser new tab. Check json data return or not.
@Charleskimani That is a problem. Laravel resource method understands show Action. So now I have updated my answer. Change route name.

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.