Angular 2 使用 http 插入 json 数据
Angular 2 inserting json data using http
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}}
我喜欢使用 post 方法向 http://localhost:3000/userinfo
添加内容。此代码不起作用 returns "unidentified: 500 error"。我需要找出这段代码有什么问题。请解决这个问题。
请提供有关数据未写入文件的原因的建议..
errormsg 显示为:
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"
}
答案很简单,我们应该像这样将 post 数据传递到 json-服务器,
让 data2=JSON.stringify({"id":1,"username":"asir","password":"007" } );
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}}
我喜欢使用 post 方法向 http://localhost:3000/userinfo
添加内容。此代码不起作用 returns "unidentified: 500 error"。我需要找出这段代码有什么问题。请解决这个问题。
请提供有关数据未写入文件的原因的建议..
errormsg 显示为:
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"
}
答案很简单,我们应该像这样将 post 数据传递到 json-服务器, 让 data2=JSON.stringify({"id":1,"username":"asir","password":"007" } );