JavaScript AJAX Fetch API和Laravel 5.8指向错误的URL(路由)

时间:2019-07-18 07:39:41

标签: javascript ajax fetch-api laravel-5.8

我想知道AJAX响应指向此URL http://www.inwrite.com/turtle/admin/login 。我进行了仔细检查,并没有更改我的 web.php 文件中的路由。我花了三天时间才发现这种错误。路线错误的可能原因是什么?

AJAX Fetch response result

这是Laravel 5.8的路线列表页面。

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

/*Route::get('/', function () {
    return view('welcome');
});*/

// prevents unauthorized access
$c = [ \App\Http\Middleware\WebAuthMiddleware::class ];
/*TEST*/
Route::get(  '/test/test2',  'Front\TestController@handle');


/*ERROR PAGE*/
//Route::get(  '/error',  'ErrorController@handle');

/*FRONT*/
Route::get(  '/',  'Front\FrontController@handle');

Route::get(  '/blogs',  'Front\BlogListController@handle');
Route::get(  '/blogs/{cat}',  'Front\BlogListController@setActiveCategory');
Route::get(  '/blogs/post/{slug}',  'Front\BlogController@handle');

Route::get(  '/page/{pg_slug}',  'Front\PageController@handle');

Route::get(  '/contact-us',  'Front\ContactUsController@handle');
Route::post(  '/send-message',  'Front\ContactUsController@sendMessage');

Route::get(  '/login',  'Front\LoginController@handle');
Route::post(  '/login',  'Front\LoginPostController@handle');
Route::get(  '/logout',  'Front\LoginController@logout');

Route::post(  '/sign-up',  'Front\SignUpController@handle');

Route::get( '/customer-verify',              'Front\CustomerVerifyController@handle');
Route::post('/customer-resend-verification', 'Front\CustomerResendVerificationController@handle');

Route::get(  '/reset-password',  'Front\ResetPasswordController@handle');
Route::post(  '/reset-password',  'Front\LoginController@resetPassword');
Route::post(  '/customer-password-reset',  'Front\ResetPasswordController@setNewPassword');

Route::get(  '/dashboard',  'Front\DashboardFrontController@handle');

Route::get(  '/create-new-order',  'Front\CreateNewOrderController@handle');
Route::post(  '/upload-files',  'Front\CreateNewOrderController@uploadFiles');

Route::get(  '/checkout',  'Front\CheckoutController@handle');

Route::post(  '/customer/edit',  'Front\CustomerEditController@handle')->middleware($c);

Route::post(  '/customer/update',  'Front\CustomerUpdateController@handle')->middleware($c);

/*ADMIN*/

Route::get(  '/admin/login',    'Dashboard\DashboardLoginController@handle');
Route::post( '/admin/login',    'Dashboard\DashboardLoginPostController@handle');
Route::get(  '/admin/logout',   'Dashboard\DashboardLogoutController@handle');

Route::get(  '/admin',  'Dashboard\DashboardController@handle')->middleware($c);

Route::get(  '/admin/orders',  'Dashboard\OrdersController@handle')->middleware($c);

Route::get(  '/admin/customers',  'Dashboard\CustomersController@handle')->middleware($c);
Route::post(  '/admin/customers/list',  'Dashboard\CustomersListController@handle')->middleware($c);
Route::post(  '/admin/customers/delete-many',  'Dashboard\CustomersDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/administrators',  'Dashboard\AdministratorsController@handle')->middleware($c);
Route::post(  '/admin/administrators/list',  'Dashboard\AdministratorsListController@handle')->middleware($c);
Route::post(  '/admin/administrators/save',  'Dashboard\AdministratorsSaveController@handle')->middleware($c);
Route::post(  '/admin/administrators/edit',  'Dashboard\AdministratorsEditController@handle')->middleware($c);
Route::post(  '/admin/administrators/delete-many',  'Dashboard\AdministratorsDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/services',  'Dashboard\ServicesController@handle')->middleware($c);
Route::post(  '/admin/services/list',  'Dashboard\ServicesListController@handle')->middleware($c);
Route::post(  '/admin/services/save',  'Dashboard\ServicesSaveController@handle')->middleware($c);
Route::post(  '/admin/services/edit',  'Dashboard\ServicesEditController@handle')->middleware($c);
Route::post(  '/admin/services/delete-many',  'Dashboard\ServicesDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/operations/business-hours',  'Dashboard\OperationsBusinessHoursController@handle')->middleware($c);
Route::post(  '/admin/operations/business-hours/list',  'Dashboard\OperationsBusinessHoursListController@handle')->middleware($c);
Route::post(  '/admin/operations/business-hours/save',  'Dashboard\OperationsBusinessHoursSaveController@handle')->middleware($c);
Route::post(  '/admin/operations/business-hours/edit',  'Dashboard\OperationsBusinessHoursEditController@handle')->middleware($c);
Route::post(  '/admin/operations/business-hours/delete-many',  'Dashboard\OperationsBusinessHoursDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/operations/dayoffs',  'Dashboard\OperationsDayoffsController@handle')->middleware($c);
Route::post(  '/admin/operations/dayoffs/list',  'Dashboard\OperationsDayoffsListController@handle')->middleware($c);
Route::post(  '/admin/operations/dayoffs/save',  'Dashboard\OperationsDayoffsSaveController@handle')->middleware($c);
Route::post(  '/admin/operations/dayoffs/edit',  'Dashboard\OperationsDayoffsEditController@handle')->middleware($c);
Route::post(  '/admin/operations/dayoffs/delete-many',  'Dashboard\OperationsDayoffsDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/operations/holidays',  'Dashboard\OperationsHolidaysController@handle')->middleware($c);
Route::post(  '/admin/operations/holidays/list',  'Dashboard\OperationsHolidaysListController@handle')->middleware($c);
Route::post(  '/admin/operations/holidays/save',  'Dashboard\OperationsHolidaysSaveController@handle')->middleware($c);
Route::post(  '/admin/operations/holidays/edit',  'Dashboard\OperationsHolidaysEditController@handle')->middleware($c);
Route::post(  '/admin/operations/holidays/delete-many',  'Dashboard\OperationsHolidaysDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/products',  'Dashboard\ProductsController@handle')->middleware($c);
Route::post(  '/admin/products/list',  'Dashboard\ProductsListController@handle')->middleware($c);
Route::post(  '/admin/products/save',  'Dashboard\ProductsSaveController@handle')->middleware($c);
Route::post(  '/admin/products/edit',  'Dashboard\ProductsEditController@handle')->middleware($c);
Route::post(  '/admin/products/delete-many',  'Dashboard\ProductsDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/file-types',  'Dashboard\FileTypesController@handle')->middleware($c);
Route::post(  '/admin/file-types/list',  'Dashboard\FileTypesListController@handle')->middleware($c);
Route::post(  '/admin/file-types/save',  'Dashboard\FileTypesSaveController@handle')->middleware($c);
Route::post(  '/admin/file-types/edit',  'Dashboard\FileTypesEditController@handle')->middleware($c);
Route::post(  '/admin/file-types/delete-many',  'Dashboard\FileTypesDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/messages',  'Dashboard\MessagesController@handle')->middleware($c);

Route::get(  '/admin/configurations',  'Dashboard\ConfigurationsController@handle')->middleware($c);
Route::post(  '/admin/configurations/list',  'Dashboard\ConfigurationsListController@handle')->middleware($c);
Route::post(  '/admin/configurations/save',  'Dashboard\ConfigurationsSaveController@handle')->middleware($c);
Route::post(  '/admin/configurations/edit',  'Dashboard\ConfigurationsEditController@handle')->middleware($c);
Route::post(  '/admin/configurations/delete-many',  'Dashboard\ConfigurationsDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/cms/header',  'Dashboard\CmsNavbarsController@handle')->middleware($c);
Route::post(  '/admin/cms/header/list',  'Dashboard\CmsNavbarsListController@handle')->middleware($c);
Route::post(  '/admin/cms/header/save',  'Dashboard\CmsNavbarsSaveController@handle')->middleware($c);

Route::get(  '/admin/cms/footer',  'Dashboard\CmsFootersController@handle')->middleware($c);
Route::post(  '/admin/cms/footer/list',  'Dashboard\CmsFootersListController@handle')->middleware($c);
Route::post(  '/admin/cms/footer/save',  'Dashboard\CmsFootersSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/footer/edit',  'Dashboard\CmsFootersEditController@handle')->middleware($c);
Route::post(  '/admin/cms/footer/delete-many',  'Dashboard\CmsFootersDeleteManyController@handle')->middleware($c);

Route::get(  '/admin/cms/header-menu-items',  'Dashboard\CmsHeaderMenuItemsController@handle')->middleware($c);
Route::post(  '/admin/cms/header-menu-items/list',  'Dashboard\CmsHeaderMenuItemsListController@handle')->middleware($c);
Route::post(  '/admin/cms/header-menu-items/save',  'Dashboard\CmsHeaderMenuItemsSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/header-menu-items/edit',  'Dashboard\CmsHeaderMenuItemsEditController@handle')->middleware($c);
Route::post(  '/admin/cms/header-menu-items/delete-many',  'Dashboard\CmsHeaderMenuItemsDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/header-menu-items/sort',  'Dashboard\CmsHeaderMenuItemsSortController@handle')->middleware($c);

Route::get(  '/admin/cms/footer-menu-items',  'Dashboard\CmsFooterMenuItemsController@handle')->middleware($c);
Route::post(  '/admin/cms/footer-menu-items/list',  'Dashboard\CmsFooterMenuItemsListController@handle')->middleware($c);
Route::post(  '/admin/cms/footer-menu-items/save',  'Dashboard\CmsFooterMenuItemsSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/footer-menu-items/edit',  'Dashboard\CmsFooterMenuItemsEditController@handle')->middleware($c);
Route::post(  '/admin/cms/footer-menu-items/delete-many',  'Dashboard\CmsFooterMenuItemsDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/footer-menu-items/sort',  'Dashboard\CmsFooterMenuItemsSortController@handle')->middleware($c);

Route::get(  '/admin/cms/sections',  'Dashboard\CmsSectionsController@handle')->middleware($c);
Route::post(  '/admin/cms/sections/list',  'Dashboard\CmsSectionsListController@handle')->middleware($c);
Route::post(  '/admin/cms/sections/save',  'Dashboard\CmsSectionsSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/sections/edit',  'Dashboard\CmsSectionsEditController@handle')->middleware($c);
Route::post(  '/admin/cms/sections/delete-many',  'Dashboard\CmsSectionsDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/sections/sort',  'Dashboard\CmsSectionsSortController@handle')->middleware($c);

Route::get(  '/admin/cms/testimonials',  'Dashboard\CmsTestimonialsController@handle')->middleware($c);
Route::post(  '/admin/cms/testimonials/list',  'Dashboard\CmsTestimonialsListController@handle')->middleware($c);
Route::post(  '/admin/cms/testimonials/save',  'Dashboard\CmsTestimonialsSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/testimonials/edit',  'Dashboard\CmsTestimonialsEditController@handle')->middleware($c);
Route::post(  '/admin/cms/testimonials/delete-many',  'Dashboard\CmsTestimonialsDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/testimonials/sort',  'Dashboard\CmsTestimonialsSortController@handle')->middleware($c);

Route::get(  '/admin/cms/faqs',  'Dashboard\CmsFaqsController@handle')->middleware($c);
Route::post(  '/admin/cms/faqs/list',  'Dashboard\CmsFaqsListController@handle')->middleware($c);
Route::post(  '/admin/cms/faqs/save',  'Dashboard\CmsFaqsSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/faqs/edit',  'Dashboard\CmsFaqsEditController@handle')->middleware($c);
Route::post(  '/admin/cms/faqs/delete-many',  'Dashboard\CmsFaqsDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/faqs/sort',  'Dashboard\CmsFaqsSortController@handle')->middleware($c);

Route::get(  '/admin/cms/top-sliders',  'Dashboard\CmsTopSlidersController@handle')->middleware($c);
Route::post(  '/admin/cms/top-sliders/list',  'Dashboard\CmsTopSlidersListController@handle')->middleware($c);
Route::post(  '/admin/cms/top-sliders/save',  'Dashboard\CmsTopSlidersSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/top-sliders/edit',  'Dashboard\CmsTopSlidersEditController@handle')->middleware($c);
Route::post(  '/admin/cms/top-sliders/delete-many',  'Dashboard\CmsTopSlidersDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/top-sliders/sort',  'Dashboard\CmsTopSlidersSortController@handle')->middleware($c);

Route::get(  '/admin/cms/external-pages',  'Dashboard\CmsExternalPagesController@handle')->middleware($c);
Route::post(  '/admin/cms/external-pages/list',  'Dashboard\CmsExternalPagesListController@handle')->middleware($c);
Route::post(  '/admin/cms/external-pages/save',  'Dashboard\CmsExternalPagesSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/external-pages/edit',  'Dashboard\CmsExternalPagesEditController@handle')->middleware($c);
Route::post(  '/admin/cms/external-pages/delete-many',  'Dashboard\CmsExternalPagesDeleteManyController@handle')->middleware($c);
Route::post(  '/admin/cms/external-pages/get-category',  'Dashboard\CmsExternalPagesGetCategoryController@handle')->middleware($c);

Route::get(  '/admin/cms/categories',  'Dashboard\CmsCategoriesController@handle')->middleware($c);
Route::post(  '/admin/cms/categories/list',  'Dashboard\CmsCategoriesListController@handle')->middleware($c);
Route::post(  '/admin/cms/categories/save',  'Dashboard\CmsCategoriesSaveController@handle')->middleware($c);
Route::post(  '/admin/cms/categories/edit',  'Dashboard\CmsCategoriesEditController@handle')->middleware($c);
Route::post(  '/admin/cms/categories/delete-many',  'Dashboard\CmsCategoriesDeleteManyController@handle')->middleware($c);


/*Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');*/

我的刀片文件的一部分:

//call AJAX to edit faqs 
        var fetchModelEdit = (id) => {
            let headers = {};
            headers['X-Requested-With'] = "XMLHttpRequest";

            var data = new FormData();
            data.append('id', id);

            fetch("{{ url('/customer/edit') }}", {
                headers: headers,
                method: "POST",
                body: data,
                credentials: "same-origin"
            }).then((res) => {
                if(res.status !== 200)
                    console.log('Failed to edit customer.'); 

                //console.log(res.clone().text());
                return res.json();
            }).then((data) => {
                //console.log(data);
                showEditModal(data);
            }).catch((e) => {
                console.log("Error! " + e);
            });
        };

1 个答案:

答案 0 :(得分:0)

中间件WebAuthMiddleware防止未经授权的访问。这只是意味着您无权访问该URL,这将导致重定向到管理员登录。

// prevents unauthorized access
$c = [ \App\Http\Middleware\WebAuthMiddleware::class ];

正如我所见,$c包含中间件,而您在评论prevents unauthorized access中提到了中间件

更改路线

Route::post('/customer/edit','Front\CustomerEditController@handle')->middleware($c);

Route::post('/customer/edit','Front\CustomerEditController@handle');

如果您希望使用Admin Auth访问url,则需要在标头中传递授权凭据。