gpt4 book ai didi

angular - http GET 工作时,angular 2 客户端中的 http POST 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:02 26 4
gpt4 key购买 nike

我正在尝试在 Angular 2 客户端(asp.net core 2.0 api 服务器)上执行 http POST 请求。

起初有一个415错误(不支持的媒体)

当我添加 header 时,出现response to preflight request doesn't pass access control check: No 'Access-Control-Origin' header is present on the requested resource 错误。

同一服务中的 Http GET 工作正常(没有 header )。

import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';

export class MyService {

constructor(private http: Http) {}

// This is working
getStudents(): Observable <string[]>{
return this.http.get('https://localhost:44753/api/students', {withCredentials:true})
.map((res:Response)=> res.Json());
}

// Return 415
getStudent(name): Observable <string[]>{
let data = {
"name": name
}
return this.http.get('https://localhost:44753/api/students', JSON.Stringify(data), {withCredentials:true})
.map((res:Response)=> res.Json());
}

// Return 401
getStudent2(name): Observable <string[]>{
let data = {
"name": name
}

let headers = new Header();
headers.append('Content-Type', 'application/json);
headers.append('Access-Control-Allow-Origin', '*');

let options = new RequestOptions({ headers:headers , withCredentials:true });

return this.http.get('https://localhost:44753/api/students', JSON.Stringify(data), options)
.map((res:Response)=> res.Json());
}

}

谁能帮我解决这个问题?

最佳答案

看来您需要为 CORS 请求配置服务器。

关于angular - http GET 工作时,angular 2 客户端中的 http POST 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287465/

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