如何使用angular 2请求数据
当我们将包含JSON内容的数据发送到外部api时,会发生如何使用angular 2请求数据,angular,angular-http,Angular,Angular Http,当我们将包含JSON内容的数据发送到外部api时,会发生accesscontrolalloworigin错误。这个问题的解决方案是使用x-www-form-urlencoded content。 是否有必要改用JSON JSON内容: this.http.post('/api/login', { email: email, password: pass }).map(res => res.json()) .subscribe(respon
accesscontrolalloworigin
错误。这个问题的解决方案是使用x-www-form-urlencoded content
。
是否有必要改用JSON
JSON内容:
this.http.post('/api/login', {
email: email,
password: pass
}).map(res => res.json())
.subscribe(response => {
console.log(response);
} , error => console.error(error));
}
x-www-form-urlencod
:
this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.options = new RequestOptions({ headers: this.headers, method: 'post'});
return this.http.post('/api/login', "email=info@mail.com&password=123", this.options )
.map(res => res.json())
.map(user => {
if (user) {
console.log(user);
}
return !!user ;
});
}
其他解决方案:
1.在Chrome中安装Access Control Allow Origin extension2.localhost中的午餐web api正在寻找另一种方法
3.在IIS7上启用CORS 但问题没有解决网络配置:
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol>
这是一个CORS问题,您需要在api@RahulSinghCORS by web.config已启用,问题未解决检查post man中的问题。默认情况下/token是在mvcXMLHttpRequest中获取令牌的url,无法加载/api/login。对飞行前请求的响应未通过访问控制检查:“访问控制允许来源”标头包含多个值“*,*”,但只允许一个值。因此,不允许访问源“”。
EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "GET,POST,PUT,DELETE");
config.EnableCors(cors);