Warm tip: This article is reproduced from stackoverflow.com, please click
ajax laravel-5.8

How to avoid user navigate to ajax url in laravel 5.8?

发布于 2020-04-11 22:40:37

I have created simple web application with laravel 5.8. this is my project url http://localhost/test/public/home

web.php

Route::group(['middleware' => ['auth', 'preventBackHistory']], function() {
   Route::get('home', 'HomeController@index')->name('home');
   Route::post('generate_table.data', 'HomeController@generate_table')->name('generate_table.data');
});

I am calling this ajax into home.blade

"ajax": { 
   "url":"{!! route("generate_table.data") !!}",
   "type": "POST",
    "jsonpCallback": 'jsonCallback',
    "dataType": "jsonp"
 }

I haven't any issue with this ajax.it is loading data. If i navigate to http://localhost/test/public/generate_table.data?callback=jsonCallback , i am getting black screen with

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

No message

If user navigate http://localhost/test/public/generate_table.data?callback=jsonCallback How to navigate back to /home?

.env APP_DEBUG=false

Thank you.

Questioner
NaramukAbus
Viewed
60
Googlian 2020-02-02 23:50

You can redirect the user when the GET method is used.

// GET method will be redirected to home.
Route::get('generate_table.data', function () {
    return redirect()->to('/');
});

// The POST method will be passed to the controller.
Route::post('generate_table.data', 'HomeController@generate_table')->name('generate_table.data');