Error with http.post and http.get with Angular 2

Asked

Viewed 167 times

1

Well, I’m having a problem sending and retrieving data with Angular 2.

Auth.ts

import {Observable} from 'rxjs/Observable';
import {Injectable} from "angular2/core";
import {Http, Response} from "angular2/http";

if (!localStorage.getItem('token')) {
   localStorage.setItem('token', 'false');
}

function fdsfds() {
   if (localStorage.getItem('token') == 'true') {
      return true;
   }else{
      return false;
   }
}
@Injectable()
export class Auth {
loggedIn: any;
result: Object;

  constructor(http:Http) {
    this.loggedIn = fdsfds();

    this.result = {friends:[]};

    http.get('oii.json').map((res: Response) => res.json()).subscribe(res => this.result = res);

  }

  login() {
    localStorage.setItem('token', 'true');
    this.loggedIn = fdsfds();

  }

  logout() {
    localStorage.setItem('token', 'false');
    this.loggedIn = fdsfds();
  }

  check() {
    return Observable.of(this.loggedIn);
  }
}

inserir a descrição da imagem aqui

  • guy checks if you step right the path of that 'oioi.json' it is on the same level of your Component? check that there

1 answer

0

The version I use is Angular 7, because it has different things, but I’ll leave my code for you to take a look and take a test.

import { Injectable } from '@angular/core';
import { baseurl } from '../../../src/app/baseurl/baseurl';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

import { Topicos } from '../interface/topicos';

@Injectable({
  providedIn: 'root'
})
export class TopicosService {

  constructor(private http: HttpClient) { }

  getList():Observable<Topicos[]> { // Pasta/Controller.php/função
    const url = `${baseurl.base_url}api_angular/topicos/topicos`;
    return this.http.get<Topicos[]>(url);
  }

  getId(id: any):Observable<Topicos> {
    const url = `${baseurl.base_url}api_angular/topicos/topicos/${id}`;
    return this.http.get<Topicos>(url);
  }

  create(topico: Topicos):Observable<Topicos> {
    const url = `${baseurl.base_url}api_angular/topicos/topicos`;
    const header = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    const body =  'nome_topico=' + topico.nome_topico +
                  '&descricao_topico=' + topico.descricao_topico +
                  '&aprova=' + topico.aprova +
                  '&ativo=' + topico.ativo +
                  '&conclui_solicitacao=' + topico.conclui_solicitacao +
                  '&inconformidade=' + topico.inconformidade +
                  '&inconformidade_tipo=' + topico.inconformidade_tipo +
                  // '&servico=' + topico.servico +
                  '&id_setor=' + topico.id_setor +
                  '&id_grupo=' + topico.id_grupo +
                  '&id_usuario=' + topico.id_usuario +
                  '&destino=' + topico.destino
                 ;
    return this.http.post<Topicos>(url, body, {headers: header});
  }

  update(topico: Topicos):Observable<Topicos> {
    const url = `${baseurl.base_url}api_angular/topicos/topicos/${topico.id_topico}`;
    const header = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    const body =  'id_topico=' + topico.id_topico +
                  '&nome_topico=' + topico.nome_topico +
                  '&descricao_topico=' + topico.descricao_topico +
                  '&aprova=' + topico.aprova +
                  '&ativo=' + topico.ativo +
                  '&conclui_solicitacao=' + topico.conclui_solicitacao +
                  '&inconformidade=' + topico.inconformidade +
                  '&inconformidade_tipo=' + topico.inconformidade_tipo +
                  // '&servico=' + topico.servico +
                  '&id_setor=' + topico.id_setor +
                  '&id_grupo=' + topico.id_grupo +
                  '&id_usuario=' + topico.id_usuario +
                  '&destino=' + topico.destino
                 ;
    return this.http.put<Topicos>(url, body, {headers: header});
  }

  delete(id_topico: number):Observable<Topicos> {
    const url = `${baseurl.base_url}api_angular/topicos/topicos/${id_topico}`;
    return this.http.delete<Topicos>(url);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.