gpt4 book ai didi

php - 在 Angular 6 中添加 Xsrf-Token 的问题

转载 作者:可可西里 更新时间:2023-11-01 00:54:28 24 4
gpt4 key购买 nike

通过 API 提交的表单数据发布成功。

但是在将 X-CSRF-TOKEN 添加到 header 并设置 withCredentials: true 之后结果数据未发布到名为 insert.php

的脚本

错误:

Failed to load http://localhost/simple_api/insert.php: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

删除 withCredentials: true 结果数据已成功发布。但是看不到X-CSRF-TOKEN

app.module.ts

import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import {HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";
import { UsrService } from './usr.service';
import { AppComponent } from './app.component';

@NgModule({
declarations: [
AppComponent,
RegisterComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',
headerName: 'X-CSRF-TOKEN'
})
],
providers: [UsrService],
bootstrap: [AppComponent]
})
export class AppModule { }

user.services.ts

import { Http, Headers, RequestOptions, Response, URLSearchParams } from '@angular/http';
addUser(info){
console.log(info);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, withCredentials: true });
console.log(options);
return this._http.post("http://localhost/simple_api/insert.php",info, options)
.pipe(map(()=>""));
}

insert.php

<?php
$data = json_decode(file_get_contents("php://input"));
header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Headers: X-CSRF-Token, Origin, X-Requested-With, Content-Type, Accept");
?>

enter image description here对照 header 的值,未设置 Xsrf-Token。我应该如何设置 Xsrf-Token 值?


更新:

import {HttpClient, HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";

constructor(private _http:HttpClient) { }

addUser(info){
console.log(info);
// let headers = new Headers({ 'Content-Type': 'application/json' });
// let options = new RequestOptions({ headers: headers, withCredentials: true });
// console.log(options);
return this._http.post("http://localhost/simple_api/insert.php",info)
.subscribe(
data => {
console.log("POST Request is successful ", data);
},
error => {
console.log("Error", error);
}
);
}

app.module.ts

import {HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";

imports: [
...
HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',
headerName: 'X-CSRF-TOKEN'
})
],
...

最佳答案

将以下 header 添加到您的 php 代码中

header("Access-Control-Allow-Credentials: true");

此外,为什么要混合使用旧的 HttpModule 和新的 HttpClient 模块? RequestOptionsHeaders 在 Angular 6 中被弃用

如果您使用 HttpClient,内容类型默认设置为 json,withCredentialsHttpClientXsrfModule 设置。

您的请求可以简化为

 return this._http.post("http://localhost/simple_api/insert.php",info);

编辑HttpClientXsrfModule 在后台创建的默认拦截器似乎不处理绝对 url....

https://github.com/angular/angular/issues/18859

关于php - 在 Angular 6 中添加 Xsrf-Token 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52342195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com