首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布到json服务器问题

发布到json服务器问题
EN

Stack Overflow用户
提问于 2016-11-17 00:10:35
回答 2查看 206关注 0票数 0

我在这方面仍然是新手,但我正在尝试将我自己的数据发布到https://jsonplaceholder.typicode.com/users上以供学习之用。在页面的底部写着使用npm install -g json-server,所以我在我的项目中运行了这个命令,我试着把数据发布到上面的网页地址,我也试着把它发布到一个静态的json文件中,我想它可以通过npm install -g json-server命令托管这个文件,但是当我使用本地文件时找不到json文件,如果我尝试发布到上面的url,那么它会在控制台日志中显示一个post,但是我的用户表从来没有更新过。谁能告诉我如何让它工作,让我可以看到我的帖子数据?

user.service.ts

代码语言:javascript
复制
      import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import {Http, Headers} from '@angular/http';
import {User} from './user';
@Injectable()
export class UserService{
constructor(private _http:Http){


}
getUsers(){
return this._http.get('http://localhost:4200/users')
.map(res=>res.json());

}
addUser(post){

return this._http.post('http://localhost:4200/users', post)
.map(res=> res.json());



}
}

form.component.ts

代码语言:javascript
复制
   import { Component, OnInit } from '@angular/core';
import {UserService} from '../users.service';
import {Http} from '@angular/http';
import {Router} from '@angular/router';
import {FormGroup, FormControl} from '@angular/forms';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css'],
  providers: [UserService]

})
export class FormComponent implements OnInit{
  private user;

  private _users;
  constructor(
    private _userService:UserService


  ){


  }

userForm = new FormGroup({
name: new FormControl(),
username: new FormControl(),
email: new FormControl(),
address: new FormGroup({
  street: new FormControl(),
  suite: new FormControl(),
  city: new FormControl(),
  postalcode: new FormControl()

})

});
onSubmit(){
this._userService.addUser(this.userForm.value)
.subscribe(res => {
this.user = res;
console.log(res);

})

}

}
EN

回答 2

Stack Overflow用户

发布于 2016-11-17 00:56:20

现在已经运行了服务器,您不需要指向app/users.json。根据documentation,你可以直接在http://localhost:3000/users上发帖。

票数 0
EN

Stack Overflow用户

发布于 2016-11-17 00:56:25

我相信你错过了公共服务(什么是合理的)

注意:资源不会真的在服务器上创建,但它会被伪装。

因为您已经在本地安装了它,所以只需运行:

$jsonplaceholder

在将您的URL主机名替换为Angular to 之后

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40636907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档