【发布时间】:2017-12-29 15:24:16
【问题描述】:
当我想用 API Symfony 和 Angular js(前端)添加表单时,我遇到了这个错误,有人知道为什么吗?
无法加载http://127.0.0.1:8000/api/users:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 使用权。 AddUserComponent.html:16 ERROR 错误:尝试区分时出错 '服务器错误'。只允许使用数组和可迭代对象 在 DefaultIterableDiffer.webpackJsonp.../../../core/@angular/core.es5.js.DefaultIterableDiffer.diff (core.es5.js:6843) 在 NgForOf.webpackJsonp.../../../common/@angular/common.es5.js.NgForOf.ngDoCheck (common.es5.js:1691) 在 checkAndUpdateDirectiveInline (core.es5.js:10846) 在 checkAndUpdateNodeInline (core.es5.js:12341) 在 checkAndUpdateNode (core.es5.js:12284) 在 debugCheckAndUpdateNode (core.es5.js:13141) 在 debugCheckDirectivesFn (core.es5.js:13082) 在 Object.eval [as updateDirectives] (AddUserComponent.html:21) 在 Object.debugUpdateDirectives [as updateDirec
用户服务
@Injectable()
export class UserService {
private uri= 'http://127.0.0.1:8000/api/users';
constructor(private http: Http, private authenticationService: AuthService
) {}
addUser(user: User) {
const headers = new Headers();
headers.append('content-type', 'application/json');
headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
return this.http.post(this.uri, JSON.stringify(user), {headers : headers})
.map(res => res.json()).catch(this.handelError);
}
【问题讨论】:
-
研究“CORS”,获得一些了解。控制跨站脚本攻击非常重要。