Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi here am trying to send some values in to the api server using services in angular2 .But unable to send..Cant detect the problem ..can any one Help please?
when i giving real api link also not working

What I have tried:

<pre>my html
<h1>Angular2 POST DEMOh1>

<p>id <input type="text" name="ID" [(ngModel)]="ID">
<p>Parameter 1 <input type="text" name="param1" [(ngModel)]="param1">
<p>Parameter 2 <input type="text" name="param2" [(ngModel)]="param2">
<p>formula 1 <input type="text" name="param3" [(ngModel)]="param3">
<p>formula 2 <input type="text" name="param4" [(ngModel)]="param4">
<p>startdate <input type="text" name="param5" [(ngModel)]="param5">
<p>enddate <input type="text" name="param6" [(ngModel)]="param6">

<button (click)="SendToApi(ID,param1,param2,param3,param4,param5,param6)">Savebutton>p>
my services
import {Injectable} from "@angular/core";
import { Http, Response, Headers, RequestOptions} from "@angular/http";
import {Observable} from "rxjs/Rx";

export class DemoService {
constructor(private _httpurl:Http){

AddDetails(params) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(params);
return' ', body, headers).map((res: Response) => res.json());

import { Component, OnInit } from '@angular/core';
import {Observable} from "rxjs/Rx";
import {DemoService} from './DemoService.service';
import { Http, Response, Headers, RequestOptions} from "@angular/http";

selector: 'my-app',
providers: [DemoService]

export class AppComponent {
//public foods;
//public new_food;
public ID;
public param1;
public param2;
public param3;
public param4;
public param5;
public param6;
constructor( private demoservice:DemoService){}

SendToApi(ID,param1,param2,param3,param4,param5,param6) {
let data = {ID: ID,param1:param1,param2:param2,param3:param3,param4:param4,param5:param5,param6:param6};
error => {
console.error("Error saving params!");
return Observable.throw(error);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900