http://localhost:3000/userinfo
[
{
"username": "simon",
"password": "password1"
},
{
"username": "bala",
"password": "password2"
},
{
"username": "prabha",
"password": "password3"
}
]
myservice.service.ts
import { Injectable } from '@angular/core';
import { Http,Response,Headers,RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
@Injectable()
export class MyserviceService {
constructor(private _http:Http) { }
addData(){
let data2=JSON.stringify({"username":"asir","password":"007" });
let headers=new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:3000/userinfo',data2,options)
.map((res:Response) =>res.json());
}
}
loginregister.ts
import { Component, OnInit } from '@angular/core';
import { Forlogin } from 'app/forlogin';
import { FormGroup, FormControl, Validators} from '@angular/forms';
import { MyserviceService } from 'app/myservice.service';
@Component({
selector: 'app-loginregister',
templateUrl: './loginregister.component.html',
styleUrls: ['./loginregister.component.css'],
})
export class LoginregisterComponent implements OnInit {
postdata:string;
url='http://localhost:3000/userinfo';
constructor(private myservice:MyserviceService) {
}
ngOnInit() {
}
onPostData(){
this.myservice.addData().subscribe(
data => this.postdata=data
);console.log("result"+this.postdata);
}
}
loginregister.html
{{postdata}}
I like to add content to http://localhost:3000/userinfo
using post method. This code does not works and returns "unidentified: 500 error". I need to find out whats wrong with this code. pls solve the issue guys.
Please provide suggestions as to why data isn't getting written to the file..
errormsg shows like that:
ERROR Object{
_body:"TypeError: Cannot read property 'id…",
status:500,
ok:false,
statusText:"Internal Server Error",
headers:Object,
type:2,
url:"http://localhost:3000/userinfo"
}
answer is simple we should pass id inorder to post data into json-server like this, let data2=JSON.stringify({"id":1,"username":"asir","password":"007" });