2

I'm a newbie in laravel. I want to add a product using ajax method but I'm getting this error, in the console: POST 404 NOT FOUND. In the network, I'm getting this error message:

{message: "", exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",…}
exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
file: "C:\Users\rolan\Desktop\Laravel Tutorial\Fims\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php"
line: 179
message: ""
trace: [{,…}, {,…}, {,…}, {,…}, {,…},…]

Here is my ajax code:

<script>
$(document).ready(function(){
    // alert("working");
    $("#btn").click(function(){
        var prodName = $("#prodName").val();
        var rate = $("#rate").val();
        var beginningQuantity = $("#beginningQuantity").val();
        var token = $("#token").val();

        $.ajax({
            type: "POST",
            data: "prodName=" + prodName + "&rate=" + rate + "&beginningQuantity=" + beginningQuantity + "&_token" + token,
            url: "<?php echo url('/saveProduct') ?>",
            success: function(data){
                console.log(data);
            }
        });
    });
});
</script>

here's the line 179 codes

<input type="hidden" value="{{csrf_token()}}" id="token">

here's the controller code

public function saveProduct(Request $request){
    return $request;
}

here's the route

Route::get('/adminIndex',"adminIndexController@count");
Route::get('/adminInventory',"adminInventoryController@count");
3
  • is that the laravel code ?!!? stackoverflow.com/questions/23430205/… Commented Apr 11, 2019 at 19:35
  • 1
    View the rendered source of your page and see what the actual url value for your ajax function is. Make sure it is what you expect it to be. Commented Apr 11, 2019 at 19:38
  • Which route is actually used to send the request to? Commented Apr 11, 2019 at 20:07

3 Answers 3

3

NotFoundHttpException usually means that your route doesn't exist. Check your web.php file and make sure that there is a route there for '/saveProduct' and that it is a post route and not a get route.

You will need a route like this:

Route::post('/saveProduct', 'adminInventoryController@saveProduct');
Sign up to request clarification or add additional context in comments.

1 Comment

Laslomcd, perhaps provide a code example of what the POST route could/should look like as example? It would help with the context of your answer.
1

Ok so I'm not sure if this has anything to do with it, but it seems as though you may have incorrectly typed it into your question, but you have written:

$.ajax({
    type: "POST",
    data: "prodName=" + prodName + "&rate=" + rate + "&beginningQuantity=" + beginningQuantity + "&_token" + token,
    url: "<?php echo url('/saveProduct') ?>",
    succes: function(data){
        console.log(data);
    }
});

Is it meant to say success: instead of succes:?

1 Comment

I edited it but still has the same problem anyways thanks
0

For me using hyphens or underscore instead of camel case did the trick

Route::post('/save-product', 'adminInventoryController@saveProduct');

Comments

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.