2
我正在尝试使post
生效,但我无法将数据发送到provider
。使用Angular 2进行POST时发生错误401
在我收到信息的页面上,我可以正确显示这些信息,但是当我尝试将它们发送给提供商时,它们会以undefined
或空的结果出现。
它还显示错误:OPTIONS HTTP://my.url/api/todo 401(未授权)和未捕获的(在承诺):网址0:响应,状态空
modal.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ReviewService } from '../../providers/review-service';
@Component({
selector: 'page-modal',
templateUrl: 'modal.html'
})
export class ModalPage {
private url: string = "http://my.url";
pessoa = [];
constructor(private _service: ReviewService) { }
peopleForm() {
console.log(this.pessoa); // Aqui eu consigo pegar as informações
this._service.novo(this.pessoa).then(res => {
console.log("Enviou para o provider");
})
}
}
审查,service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ReviewService {
private url = 'http://my.url/post';
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
private headers = new Headers({'Content-Type': 'application/json'});
constructor(public http: Http) { }
novo(pessoa: Array<string>) {
console.log("Exibir o que recebeu: " + pessoa); // Aqui me retorna undefined ou vazio
return this.http
.post(this.url, JSON.stringify(pessoa), {headers: this.headers})
.toPromise()
.then(res => res.json().data)
.catch(this.handleError);
}
}
看起来你需要追踪的是授权问题,看来你的代码是不允许访问的待办事项服务的原因之一或另一个(这可能是一些事情)。无论它是轮胎爆胎,空油箱还是与树相撞,Angular 2都会令人沮丧地显示“承诺中未被捕获的错误”(据我所知)的一切,有点像“汽车运动中的错误”。所以我不会把这当作根本问题。 –
查看网络日志,似乎跨服务器端的域访问没有正确设置。对OPTIONS的调用应该成功。 – Chandermani