无法使用带有解决方案的代理配置在angular中对HttpClient执行post请求
当我试图在无法使用带有解决方案的代理配置在angular中对HttpClient执行post请求,angular,httpclient,Angular,Httpclient,当我试图在HttpClient中发出post请求时,得到下面的错误,而rest客户端也给出了正确的响应。与angular 1相同,$http服务正在按预期工作 尝试了多种方法,但post或get方法均无效。 我正在使用angular cli,我在其中配置了proxy.config.json { "/api/*":{ "target":"http://10.104.40.14:8290/my_app", "secure":false, "l
HttpClient
中发出post请求时,得到下面的错误,而rest客户端也给出了正确的响应。与angular 1相同,$http
服务正在按预期工作
尝试了多种方法,但post或get方法均无效。
我正在使用angular cli,我在其中配置了proxy.config.json
{
"/api/*":{
"target":"http://10.104.40.14:8290/my_app",
"secure":false,
"logLevel":"debug"
}
}
//错误代码
zone.js:2933 POST http://localhost:4200/api/security/login 401 (Unauthorized)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 401 Unauthorized</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /de_prp/error. Reason:
<pre> Unauthorized</pre></p>
</body>
</html>
解决方案:通过下面给出的proxy.config.json中的条目最终解析
“pathRewrite”:{“^/api”:“}所以最终的json文件是
试着这样写你的发帖请求:
登录(用户名、密码、记忆)){
常量头=新头();
headers.append('Content-Type','application/json');
//你可以在这里附加任何你喜欢的标题;
headers.append(用户名、密码);
选项:RequestOptions=新的RequestOptions(标题);
this.http.post(/api/security/login,options)
.suscribe(res=>res.json())
}
401
是来自处理请求的服务器的错误。您需要查看服务器是否正在获取请求的标题
关于代理配置,您可以执行以下操作:
{”/api:{“target”:“yourUrl.com”,
“安全”:错误,
“路径重写”:{“^/api”:“}
}
}
pathRewrite
选项将从您的url中删除api
。您能否使用浏览器中的开发人员工具,查看您的请求是否已发出?您可以查看您的post请求是否包含您传递给它的标题。请求URL:请求方法:post状态代码:401未经授权的远程地址:127.0.0.1:4200引用方策略:降级时无引用响应标题查看源访问控制允许来源:*缓存控制:必须重新验证,无缓存,无店铺连接:关闭内容类型:text/html;charset=iso-8859-1日期:2017年10月24日星期二15:01:27 GMT传输编码:分块www验证:承载x-application-context:application:production:8290 x-Powered-By:ExpressRequest Headers查看源接受:application/json,text/plain,/接受编码:gzip,deflate,br接受语言:en-US,en;q=0.8连接:保持活动内容长度:61内容类型:text/plain X-Request-With:XMLHttpRequest请求有效负载视图源{username:admin@example.com,密码:“a123456”}密码:“a123456”用户名:admin@example.com"名称当我点击get请求时,它给出了正确的响应,但不确定post为什么创建了一个问题。这可能与您的post头的格式有关。我使用的是httpClient,因此需要将负载作为用户名和密码传递,头作为const headers=new HttpHeaders({'X-request-With':'XMLHttpRequest'});以及内容类型。如何设置多个标头属性这是angular 1请求标头请求URL:请求方法:POST状态代码:200 OK远程地址:[::1]:5002引用者策略:降级时无引用使用RequestOptions
可以在请求中指定{'X-Requested-With':'XMLHttpRequest'}
。我编辑了我的答案。我根据@RahulKumarJain编辑了问题,你可以为它打开一张新的SO票
import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { RequestOptions, Headers } from '@angular/http';
import { URLSearchParams } from '@angular/http';
@Injectable()
export class AuthService implements OnInit {
constructor(private http: HttpClient) { }
ngOnInit(): void {
}
login(username, password, rememberMe) {
console.log(username, password, rememberMe);
//const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
const body = JSON.stringify({ username: username, password: password });
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
this.http.post("/api/security/login", body, { headers: headers }).subscribe(
res => {
console.log(res);
},
(err: HttpErrorResponse) => {
console.log(err.error);
console.log(err.name);
console.log(err.message);
console.log(err.status);
}
)
}
logout() {
this.http.get("/api/auth/logout").subscribe(
res => {
console.log(res);
},
(err: HttpErrorResponse) => {
console.log(err.error);
console.log(err.name);
console.log(err.message);
console.log(err.status);
}
);
}
}
{
"/api/*":{
"target":"http://10.104.40.14:8290/my_app",
"secure":false,
"pathRewrite": {"^/api" : ""}
}
}