温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How to avoid user navigate to ajax url in laravel 5.8?
ajax laravel-5.8

其他 - 如何避免用户导航到laravel 5.8中的ajax网址?

发布于 2020-04-12 12:28:29

我使用laravel 5.8创建了简单的Web应用程序。这是我的项目网址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');
});

我称这个ajax为home.blade

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

我对此ajax没有任何问题。它正在加载数据。如果我导航到http://localhost/test/public/generate_table.data?callback = jsonCallback,我将收到黑屏

Symfony \组件\ HttpKernel \异常\ MethodNotAllowedHttpException

没有讯息

如果用户浏览http://localhost/test/public/generate_table.data?callback = jsonCallback如何导航回/ home?

.env APP_DEBUG=false

谢谢。

查看更多

提问者
NaramukAbus
被浏览
29
Googlian 2020-02-02 23:50

使用GET方法时,您可以重定向用户。

// 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');