【问题标题】:Laravel passport consuming api with js not workingLaravel 护照消费 api 与 js 不工作
【发布时间】:2018-09-20 10:13:09
【问题描述】:

我正在尝试使用 ajax (axios) 请求来使用我的 api,就像 laravel 文档 here 中指示的那样。但它不起作用,显示如下图所示的错误

我的示例标头响应如下所示:

我的 ajax 请求如下所示:

   axios.get('http://localhost:81/test_laravel/public/api/user')
            .then(function(response){
                console.log('Component mounted.')
            })
            .catch(function(error){
                console.log(error.response.status)
            });

不知道发生了什么。请帮忙。

【问题讨论】:

  • @iCoders 我已经更新了我的问题。
  • js 消费者不需要标头
  • 可能您需要通过身份验证才能访问该 api
  • @Faramarz 我需要通过什么认证?你知道有什么办法吗?
  • 我没有使用 api auth 但你可以在文档中找到它:laravel.com/docs/5.7/authentication

标签: ajax laravel laravel-passport


【解决方案1】:

如果我像这样在 \App\Http\EncryptCookies.php 文件中进行更改,这将有效:

<?php

namespace App\Http\Middleware;

use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected static $serialize = true;

    protected $except = [
        //
    ];

    public function __construct(EncrypterContract $encrypter)
    {
        parent::__construct($encrypter);
    }
}

通过添加protected static $serialize = true; 行来更改$serializable 的值。

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2019-03-01
    • 1970-01-01
    • 2021-01-01
    • 2018-10-09
    • 2015-07-03
    相关资源
    最近更新 更多