saimcan
saimcan

Reputation: 1762

jQuery Post Returns Page Source (laravel)

I'd like to post a route and log the returned data. But it seems data returns the current page html.

JS

$(document).ready(function(){
    $('#approvalButtonID').click(function(){
        $.when(
            $.post("myRoute", function(data){
                console.log(data);
            })
        ).then(function(){
            //location.reload();
        });
    });
});

routes.php

Route::post('myRoute', array(
    'as' => 'myRoute',
    'uses' => 'myController@myFunction'
));

mycontroller.php

public function myFunction(){
   return 'KyloRenIsAKiller';
}

Thanks in advance.

Upvotes: 0

Views: 55

Answers (1)

Don't Panic
Don't Panic

Reputation: 14520

Maybe the normal HTML submit is happening after your jQuery .click handler. So the form POSTs, and if you have no action specified it will post to the current page, which will likely just return the same current page, which explains what you're seeing.

Try adding:

$('#approvalButtonID').click(function(e){
    e.preventDefault();
    // ... rest of your code

Upvotes: 1

Related Questions