2016-12-30 134 views
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); 
    } 

} 
+0

看起來你需要追蹤的是授權問題,看來你的代碼是不允許訪問的待辦事項服務的原因之一或另一個(這可能是一些事情)。無論它是輪胎爆胎,空油箱還是與樹相撞,Angular 2都會令人沮喪地顯示「承諾中未被捕獲的錯誤」(據我所知)的一切,有點像「汽車運動中的錯誤」。所以我不會把這當作根本問題。 –

+0

查看網絡日誌,似乎跨服務器端的域訪問沒有正確設置。對OPTIONS的調用應該成功。 – Chandermani

回答

0

你可以這樣做服務驗證

import { CanActivate, Router } from '@angular/router'; 
import { Injectable } from '@angular/core'; 

/** 
* Class that deals with redirect to login if user tries to 
* access any route without being logged in. 
**/ 
@Injectable() 
export class CanActivateAuthGuard implements CanActivate { 

    constructor(private router: Router) { } 

    /** 
    * Overrides abstract methods that grants the permison to access a url 
    * if token is stored in Angular Local Storage. 
    **/ 
    canActivate(): boolean { 
    let currentUser = JSON.parse(localStorage.getItem('currentUser')); 
    if (currentUser) { 
     return true; 
    } else { 
     this.router.navigate(['/login/login']); 
     return false; 
    } 
    } 
} 

在每個routing.module使用它:

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 
import { CanActivateAuthGuard } from '../shared/auth/auth.service'; 

const routes: Routes = [ 
    { 
    path: '', 
    canActivate: [CanActivateAuthGuard], 
    data: { 
     title: 'General User' 
    }, 
    children: [ 
    ] 
    } 
]; 

@NgModule({ 
    imports: [RouterModule.forChild(routes)], 
    exports: [RouterModule] 
}) 
export class GeneralRoutingModule {} 
相關問題