仅使用 Laravel 作为 API 时,通过 Angular 4 传递 csrf 元标记

问题描述 投票:0回答:1

我正在使用 Laravel 5.5 和 Angular 4。Laravel 仅作为 API。我正在尝试从表单传递数据,但无法传递 csrf 令牌。 这是我的服务:

import { ElementRef, Injectable, Injector, Renderer2, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Link } from './link.model';
import 'rxjs/Rx';
import { RequestOptions } from '@angular/http';

@Injectable()
export class LinkService {

    constructor(private http: HttpClient) {

    }

    createLink(link): Observable<any> {
    //links param is the data from the form
      
        let url = 'http://127.0.0.1:8000/links';
        return this.http
            .post(url, link)
            .map(response => {
                console.log(response);
            })
    }
}

我已经从App/Kernel.php中删除了VerifyCsrfToken中间件,所以现在没有错误了。但如果有人知道是否可以通过,请告诉我。

angular laravel csrf
1个回答
2
投票

VerifyCsrfToken 中间件在每个 POST 请求中都需要一个 Laravel 会话和一个 X-CSRF-TOKEN 标头。默认情况下,laravel 会发送带有 GET 响应的 X-XSRF-TOKEN cookie,以便您可以获取该值并实现 Angular HttpInterceptor 以在请求中设置标头!

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.