【问题标题】:Axios returning HTML in response instead of dataAxios 在响应中返回 HTML 而不是数据
【发布时间】:2021-04-18 04:46:03
【问题描述】:

我想就 Axios 的 对我通过 Laravel 登录路径中的 POST 请求的响应寻求帮助。当它对用户进行身份验证(或在数据库中找到用户)时,它会在数据中返回主页的 HTML。请看下面的回复截图:

我可以要求解决我的问题吗?以下是我正在使用的必要代码和版本:

工具

VueJS:v3.0.5 爱讯:0.19 Laravel:5.8

代码

Login.vue(脚本)

import axios from 'axios'

export default {
    data() {
        return {
            csrfToken: '',
            loginObj: {
                username: '',
                password: '',
                remember: false,
            },
            message: '',
        }
    },
    created() {
        this.csrfToken = document.querySelector('meta[name="csrf-token"]').content;
    },
    methods: {
        signIn() {
            console.log(this.loginObj)

            axios.post('login', this.loginObj).then(res => {
                console.log(res)
            })
            .catch(error => {
                console.log(error.response)
            })
        }
    }
}

Login.vue(模板)

<div class="card shadow-lg border-0 rounded-lg mt-5">
    <div class="card-header bg-orange"><h3 class="text-center my-4">Hello!</h3></div>
    <div class="card-body p-0">
        <div class="row">
            <div class="col-lg-6 d-none d-lg-block">
                <div class="login-image-wrap text-center">
                    <img src="/img/ahrs_logo_trans.png" class="login-img" alt="AKB Logo">
                </div>
            </div>
            <div class="col-lg-6">
                <div class="p-5">
                    <div class="text-center">
                        <h1 class="h4 text-gray-900 mb-4">Sign In</h1>
                    </div>
                    
                    <form name="loginForm" @submit.prevent="signIn">
                        <input type="hidden" name="_token" :value="csrfToken">
                        <div class="form-group">
                            <label class="small mb-1" for="inputUsername">Username</label>
                            <input class="form-control py-4" v-model="loginObj.username" id="inputUsername" type="text" placeholder="Enter username" required autofocus>
                        </div>
                        <div class="form-group">
                            <label class="small mb-1" for="inputPassword">Password</label>
                            <input class="form-control py-4" v-model="loginObj.password" id="inputPassword" type="password" placeholder="Enter password" required>
                        </div>
                        <div class="form-group">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="remember" id="remember" v-model="loginObj.remember">

                                <label class="form-check-label text-small" for="remember">
                                    Remember Me
                                </label>
                            </div>
                        </div>
                        <div class="form-group d-flex align-items-center justify-content-between mt-4 mb-0">
                            <a class="small" href="password.html">Forgot Password?</a>
                        </div>
                        <hr />
                        <div>
                            <button type="submit" class="btn bg-yellow btn-block font-weight-bold">LOGIN</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

登录控制器.php

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}

// override in the AuthenticatesUsers function username()
public function username()
{
    return 'username';
}

感谢您的帮助。

【问题讨论】:

  • 你使用的是 Laravel 自带的默认 bootstrap.js 文件吗?您是否希望它返回用户 JSON 数据?

标签: laravel axios vuejs3


【解决方案1】:

如果是 ajax 请求,您可能需要告诉 Laravel 您希望将用户返回为 JSON。

您可以通过将以下内容添加到您的 LoginController 来做到这一点:

protected function authenticated(Request $request, $user)
{
    if ($request->ajax()) {
        return $user;
    }
}

您可能还需要设置通常在 Laravel 附带的默认 bootstrap.js 文件中设置的 X-Requested-With 标头,以便 Laravel 知道这是一个 ajax 请求:

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

显然,如果该行已经存在并且您仍在捆绑包中包含引导文件,则可以忽略它。

【讨论】:

  • 你好。是的。我在 Laravel (resources/js/bootstrap.js) 中使用 bootstrap.js,它有以下代码:
  • window.axios = require('axios');
  • window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  • 我可以问一下在 LoginController 中放什么来告诉 Laravel 我想要用户为 JSON 吗?
  • 对不起@MarkJerlyBundalian,当我将authenticated() 方法添加到我的答案时,我一定没有点击保存。我现在已经更新了。此外,由于您在 window 对象上设置了 axios,因此您也不需要将其导入到您的组件中。
猜你喜欢
  • 1970-01-01
  • 2020-10-09
  • 2020-10-06
  • 2021-10-19
  • 2018-10-11
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
相关资源
最近更新 更多