2016-12-30 3 views
2

postを作成しようとしていますが、providerにデータを送信できません。角度2のPOSTを実行するとエラー401

私は情報を受け取るページで正しく表示することができますが、プロバイダに送信しようとするとundefinedまたは空の結果になります。

また、エラーが表示されます。OPTIONSます。http://my.url/api/todo 401(無許可)キャッチされない(約束で):ステータス応答:URL 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

が見える、あなたのコードは、1つの理由でそのTODOサービスにアクセスすることを許可されていないようです(それはいくつかのことがあります)。角度2はフラットタイヤ、空のガスタンク、または木との衝突にかかわらず、(「見ることができる限り」、「プロミスのキャッチされていないエラー」をフラストレーションで表示します。だから私は根本的な問題としてそれに焦点を当てません。 –

+0

ネットワークログを参照してください。クロスドメインアクセスがサーバー側で正しくセットアップされていないようです。 OPTIONSの呼び出しは成功するはずです。 – Chandermani

答えて

0

サービスのように認証することができます。各routing.module

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; 
    } 
    } 
} 

は、それを使用する:あなたはその承認問題を追跡する必要があるよう

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 {} 
関連する問題