mario
mario

Reputation: 1543

Jquery Ajax in Laravel 4 - NotFoundHttpException

.Hello y'all. I'm trying to learn ajax and am currently trying to have an input in a form be sent via a route to a controller and then echoed in a div.

For some reason I am getting a NotFoundHttpException error when I press the submit button for the form and am having trouble seeing what is wrong with my route.

If anyone can point out where I'm going wrong it would be greatly appreciated! Thank you!

View:

<form id="edit_price_form" method="POST" action=""> 
    <input name="new_price" id="new_price"/>
    <input type='submit' class='button tiny radius' id='edit_price_button'/>
</form>

Ajax request:

$(document).ready(function(){
    $("#edit_price_form").submit(function(e) {
        e.preventDefault();

        //form_data
        var form_data = $('#edit_price_form').serializeArray();

        $.ajax({
            url: 'edit_prices',
            type: "POST",
            dataType: "html",
            data: form_data,
            success: function(data){
                $("#edit_results").html(data);
                $("#edit_results").addClass('panel callout radius');
            },
            error: function(xhr, status, error){
                console.log(xhr);
                console.log(status);
                console.log(error);
            }
        });
    });
});

Route:

/*Ajax Edit Price on Price Page*/
    Route::post('edit_prices', array(
        'as'    => 'edit_prices',
        'uses'  => 'PriceController@edit_prices'        
    ));

Controller:

public function edit_prices(){

        $new_price = Input::get('new_price');

    echo $new_price;
}

Thanks again!

Upvotes: 0

Views: 311

Answers (1)

Kryten
Kryten

Reputation: 15740

The problem lies in your HTTP method. You're AJAX request looks like this:

$.ajax({
    url: 'edit_prices',
    type: "PUT",
    ...

But your route looks like this:

Route::post('edit_prices', array(
    'as'    => 'edit_prices',
    'uses'  => 'PriceController@edit_prices'        
));

In short, you'll need to change your AJAX request from PUT to POST - Laravel is rejecting the PUT request because it's expecting POST.

Upvotes: 1

Related Questions