【问题标题】:404 not found error with Laravel8 When I use api.php当我使用 api.php 时,Laravel8 找不到 404 错误
【发布时间】:2021-09-08 17:14:43
【问题描述】:

我正在使用 Laravel 8 制作简单的甘特图,但我遇到了问题。 当我进行路由时,打开页面时出现 404 错误。 顺便说一句,我正在使用 xampp。 另外,我是 Laravel 初学者,所以参考了某个页面的教程。 https://docs.dhtmlx.com/gantt/desktop__howtostart_php_laravel.html

[错误日志]

GET http://localhost/api/data?dhxr1624612640474=1 404 (Not Found)
(anonymous) @ ajax.js:221
e._execute @ bluebird.js:1164
D._resolveFromExecutor @ bluebird.js:3707
D @ bluebird.js:3258
_call @ ajax.js:176
query @ ajax.js:133
get @ ajax.js:137
t.load @ load.js:21
(anonymous) @ gantt:25

[查看]gantt.blade.php

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
 
    <script src="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js"></script>
    <link href="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css" rel="stylesheet">
 
    <style type="text/css">
        html, body{
            height:100%;
            padding:0px;
            margin:0px;
            overflow: hidden;
        }

    </style>
</head>
<body>
<div id="gantt_here" style='width:100%; height:100%;'></div>
<script type="text/javascript">
    gantt.config.date_format = "%Y-%m-%d %H:%i:%s";
 
    gantt.init("gantt_here");
  
    gantt.load("/api/data");
</script>
</body>

[控制器]GanttController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Task;
use App\Models\Link;

class GanttController extends Controller
{
    public function get(){
        $tasks = new Task();
        $links = new Link();
 
        return response()->json([
            "data" => $tasks->all(),
            "links" => $links->all()
        ]);
    }
}

[路线]api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get('/data', 'GanttController@get');

我自己无法解决,但我想这是因为路由。谁能帮帮我?

【问题讨论】:

  • 在 curl 请求中添加标头 -H "accept: application/json".

标签: php ajax laravel laravel-8


【解决方案1】:

确保您的 xampp 的 apache 和 mysql 服务正在运行,建议以新方式而不是旧方式定义您的路由, 因此,首先启动您的 xampp 服务并进行更改:

Route::get('/data', 'GanttController@get');

进入

Route::get('/data', [GanttController::class, 'get']);

这是laravel 8.x 中的新功能,然后查看您的代码,您正在创建TaskLink 的新实例,并试图从中获取数据,这是错误的方法。

你应该得到这样的数据:

$tasks = Task::all();
$links = Link::all();

【讨论】:

  • 按照你告诉我的修改路线后,我解决了这个问题。我看到语法在 Laravel 8 中是新的。我将更详细地阅读官方文档。非常感谢
猜你喜欢
  • 2017-06-14
  • 1970-01-01
  • 2019-12-06
  • 2013-10-07
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多