gpt4 book ai didi

angular - net::ERR_CONNECTION_REFUSED 与 angular2 中的 http post

转载 作者:行者123 更新时间:2023-12-04 17:50:49 24 4
gpt4 key购买 nike

我收到以下 POST 请求错误:

POST http://127.0.0.1:port/v1/route2 net::ERR_CONNECTION_REFUSED

GET 请求运行良好。 POST 失败的可能原因是什么? testPost有没有错误?
这是 Angular 服务的一部分:

@Injectable()
export class SampleService {
private _getUrl: string = 'http://<server-ip>/v1/route1';
private _postUrl: string = 'http://127.0.0.1:<port>/v1/route2';
constructor(private _http: Http) {}
testGet() {
return this._http.get(this._getUrl).map((response: Response) => response.json());
}
testPost() {
const headers = new Headers();
headers.append('Content-Type', 'application/json; charset=utf-8');
this._http.post(this._postUrl, JSON.stringify({'key1': 'value1', 'key2': 'value2'}), headers).subscribe(() => {}, err => console.error(err));
}
}

最佳答案

我认为问题在于您如何将 header 传递给请求。 header 应包装在 RequestOptions 对象中。此外,您不需要将 JSON 对象字符串化,因为您将 Content-Type 设置为 JSON

testPost() {
const headers = new Headers();
headers.append('Content-Type', 'application/json; charset=utf-8');
const options = new RequestOptions({ headers: headers });
this._http.post(this._postUrl, {'key1': 'value1', 'key2': 'value2'}, options)
.subscribe(() => {}, err => console.error(err));
}

关于angular - net::ERR_CONNECTION_REFUSED 与 angular2 中的 http post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058385/

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