简体   繁体   中英

Laravel with Jquery - Ajax Throwing 500 Error - Prob with Server Side

. Hello y'all. I am trying to gather a few variables and send it to my controller. I keep getting a 500 error and can't figure out where exactly I'm going wrong other than I'm pretty sure its server side. Any pointers about where I went wrong or better practices would be greatly appreciated! Thank yall so much!

Route:

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

Controller:

public function price_edit(){

    console.log($id_and_db);

    }

JS:

/*Ajax edit prices*/
$(document).ready(function(){

        $('.edit_button').click(function(e){
        e.preventDefault();

        var id_and_db   = $(this).prop('name').replace('edit', 'newprice'),
        new_price   = $('[name=' + id_and_db + ']').val();

        $('#test').val(id_and_db);

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

});

Error Message:

POST http://localhost/local/example/public/edit_price 500 (Internal Server Error) jquery.min.js:4
XHR finished loading: POST "http://localhost/local/example/public/edit_price". jquery.min.js:4
Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
 price_index_admin.js:40
error price_index_admin.js:41
Internal Server Error 

You did

'uses'  => 'PriceController@edit_price'

but your controller method is price_edit() .

Try change your controller method to

public function edit_price() {

This is not valid for a controller - it looks like you are trying to run java inside php:

public function price_edit(){

    console.log($id_and_db);

    }

it should be something like this

public function price_edit(){

    return Response::json(['your response here']);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM