【发布时间】:2018-04-09 20:17:28
【问题描述】:
我使用 Angular 5 前端和 Laravel 5.6 后端作为 Rest API
现在我正在尝试使用以下方式获取访问令牌:
const url = 'http://127.0.0.1:8000/oauth/token' ;
const body = JSON.stringify({
'client_id' : '8',
'client_secret': 'nMfgx0XrlfvImZK1vqu7PucCaaezDZofJOhTLh4m',
'grant_type' : 'password',
'username' : 'admin',
'password' : '0a09943d507c591ae7269042a57db16ac9a2b971'
});
const httpOptions = {
headers: new HttpHeaders({
'Accept' : 'application/json',
'Content-Type': 'application/json'
}
)
};
const res = this.http.post<any>(url, body, httpOptions)
.subscribe(myres => { console.log(myres); }) ;
它在 PostMan 上运行良好,但使用 Angular 显示此错误:
login:1 加载失败http://127.0.0.1:8000/oauth/token: 响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。
我听说应该使用一个叫做:
header('Access-Control-Allow-Origin', '*')ce
【问题讨论】:
-
您是否尝试搜索例如“laravel 启用 CORS”?
-
是的,我找到了这个标题('Access-Control-Allow-Origin', '*')
标签: ajax angular laravel rest httprequest