【问题标题】:Laravel controller returning index view instead of json responseLaravel 控制器返回索引视图而不是 json 响应
【发布时间】:2021-03-08 17:45:26
【问题描述】:

我正在使用 apache2 和 php artisan serve 命令来测试我的项目。

拥有下一个控制器:

<?php
// file: Http\Controllers\AuthCtrl.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User; //  <--
use Illuminate\Support\Facades\Auth;

class AuthCtrl extends Controller
{
    public function login(Request $request)
    {
        $rules = [ 'email' => 'required|exists:users',
                   'password'  => 'required' ];

        $request->validate($rules);

        $data = [ 'email' => $request->get('email'),
                  'password'  =>  $request->get('password') ];

        if( Auth::attempt($data) )
        {
            $user = Auth::user();
            // the $user->createToken('appName')->accessToken generates the JWT token that we can use
            return response()->json([
                                      'user'  =>  $user, // <- we're sending the user info for frontend usage
                                      'token' =>  $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
                                      'success' => true
                                    ]);
        }
        else
        {
            return response()->json([
                                    'success' => false,
                                    'msg' => 'Credenciales erroneas.'
                                    ]);
        }

    }
}

还有一个简单的模拟 index.php,它使用以下 js 进行 ajax 调用:

function ExecLogin()
{
    $.ajax({
        method: "POST",
        url: "http://127.0.0.1:8000/api/login",
        data : {email : "fsd@gmail.com", password: "mypass"}
    }).done(
    function(data, status) 
    {
        if(data.success == true)
        {
            localStorage.setItem('appname_token', data.token);
            // the following part makes sure that all the requests made later with jqXHR will automatically have this header.
            $( document ).ajaxSend(function( event, jqxhr, settings ) {
                jqxhr.setRequestHeader('Authorization', "Bearer " + data.token); 
            });
        }
        else
        {
            alert(data.msg);
        }
    }).fail(function(error){
        // handle the error
    });
} 

如您所见,当代码进入这部分时,(当用户认证OK时),它返回一个JSON响应就可以了:

$user = Auth::user();
            // the $user->createToken('appName')->accessToken generates the JWT token that we can use
            return response()->json([
                                      'user'  =>  $user, // <- we're sending the user info for frontend usage
                                      'token' =>  $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
                                      'success' => true
                                    ]);

但是当没有认证OK的时候,在这部分进入,但是响应的是index.php text/html,而不是我定义的json响应:

return response()->json([
                          'success' => false,
                          'msg' => 'Credenciales erroneas.'
                                    ]);

我在 ajax 调用的数据对象上收到以下字符串:

"<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 200;
                height: 100vh;
                margin: 0;
            }

            .full-height {
                height: 100vh;
            }

            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }

            .position-ref {
                position: relative;
            }

            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }

            .content {
                text-align: center;
            }

            .title {
                font-size: 84px;
            }

            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 13px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }

            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
                            <div class="top-right links">
                                            <a href="http://127.0.0.1:8000/login">Login</a>

                                                    <a href="http://127.0.0.1:8000/register">Register</a>
                                                            </div>
            
            <div class="content">
                <div class="title m-b-md">
                    Laravel
                </div>

                <div class="links">
                    <a href="https://laravel.com/docs">Docs</a>
                    <a href="https://laracasts.com">Laracasts</a>
                    <a href="https://laravel-news.com">News</a>
                    <a href="https://blog.laravel.com">Blog</a>
                    <a href="https://nova.laravel.com">Nova</a>
                    <a href="https://forge.laravel.com">Forge</a>
                    <a href="https://vapor.laravel.com">Vapor</a>
                    <a href="https://github.com/laravel/laravel">GitHub</a>
                </div>
            </div>
        </div>
    </body>
</html>
"

我在这里遗漏了什么重要的东西吗?

身份验证中间件如下所示:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson() ) {
            return route('login');
        }
    }
}

【问题讨论】:

  • 您是如何检查响应 id text/html 的?您是否查看了对/api/login 的响应或ajax 调用?检查当带有无效凭据的发布请求发送到 /api/login 时返回的响应是什么 - 在 devtools 的网络选项卡中并通过控制台登录 index.php 的 js sn-p
  • 我就是这么做的,它返回 index.php 的 html 页面。让我把它放在这个问题上。
  • 就是这样,我以前在生产服务器上从来没有遇到过这个问题,所以我想这一定是本地开发的 php artisan 服务。
  • 查看App\Http\Middleware\AuthenticateredirectTo()的内容是什么
  • 受保护的函数 redirectTo($request) { if (!$request->expectsJson() ) { return route('login'); } }

标签: jquery laravel apache2


【解决方案1】:

找到罪魁祸首。 首先,我尝试设置响应标头,因为:

if (! $request->expectsJson() )

所以请求只接受 json 响应。 然后我得到了一个:

POST http://127.0.0.1:8000/api/login 422 (Unprocessable Entity)

所以我去查看了控制器中的验证器:

改变了这个:

$rules = [ 'email' => 'required|exists:users',
           'password'  => 'required' ];

到这里:

$rules = [ 'email' => 'required',
           'password'  => 'required' ];

它就像一个魅力。 我认为验证需要一个已经存在的用户对象,但我传递了一个不存在的用户(错误的凭据)。 我想这就是问题所在。

【讨论】:

    【解决方案2】:

    我在 API 中开发 create 操作时遇到了同样的问题。 我想问题在这里得到了解答:https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors.

    直截了当:“那么,如果传入的请求字段没有通过给定的验证规则怎么办?如前所述,Laravel 会自动将用户重定向回他们之前的位置。此外,所有验证错误和请求输入都会自动闪现到会话中”。

    考虑到我还没有创建任何前端,没有自定义视图,所以 Laravel 将请求重定向到标准的“欢迎”视图。

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 2012-04-25
      • 2017-01-28
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2018-10-14
      • 2015-10-15
      相关资源
      最近更新 更多