简体   繁体   English

angular2 http.post() 到本地 json 文件

[英]angular2 http.post() to local json file

little help?帮助不大?

I am trying to write pure JSON to a file inside my project, my project tree looks like this:我想纯JSON写信给我的项目中的一个文件,我的项目树是这个样子:

src
->app
-->components
--->people.component.ts
--->(other irrelevant components)
-->services
--->people.service.ts
-->data
--->JSON.json
->(other irrelevant src files)

The code in my people.component.ts is just used to call and subscribe to a function inside my people.service.ts , passing the newPerson property which is data-binded from the DOM using angular2 magic;在我的代码people.component.ts只是用来通话和订阅我的内部功能people.service.ts ,传递newPerson属性,它是使用魔法angular2从DOM数据绑定; this is the function inside name.service.ts :这是name.service.ts的函数:

public addPerson(newPerson) {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.post('app/data/PEOPLE.json', newPerson, { header: headers })
        .map(res => res.json())
}

My objective is to write (if necessary replace) the newPerson property (or the entire file) inside PEOPLE.json .我的目标是写(必要时更换)的newPerson内的财产(或整个文件) PEOPLE.json of course, the current addPerson() function returns an error.当然,当前的addPerson()函数返回错误。

I've also tried the put method, it errors slightly differently, but i haven't found solutions to either method.我也试过put方法,它的错误略有不同,但我还没有找到任何一种方法的解决方案。

I know categorically it's not an error with the format/type of data i'm trying to put in PEOPLE.json file.我清楚地知道这不是我试图放入PEOPLE.json文件的数据格式/类型的错误。

Unfortunately, you can not PUT or POST directly to a file on your local filesystem using client side (browser) JavaScript.不幸的是,您不能使用客户端(浏览器)JavaScript 直接 PUT 或 POST 到本地文件系统上的文件。

Consider building a simple server to handle a POST request.考虑构建一个简单的服务器来处理 POST 请求。 If you are most comfortable with JavaScript, try Express with Node.js如果您最熟悉 JavaScript,请尝试使用 Node.js 进行 Express

// server.js
'use strict'

const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');

const app = express()
app.use(bodyParser.json());

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:8000');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  res.header('Access-Control-Allow-Methods', 'POST');
  next();
});

app.post('/', (req, res) => {
  console.log('Received request');
  fs.writeFile('json.json', JSON.stringify(req.body), (err) => {
    if (err) throw err;
    console.log('File written to JSON.json');
    res.send('File written to JSON.json')
  })
});

app.listen(3000, ()=>{
  console.log('Listening on port 3000. Post a file to http://localhost:3000 to save to /JSON.json');
});

You cannot write to files from Browser.您不能从浏览器写入文件。 Period.时期。 Even if such action is possible it will be stored on User side, not your server.即使可能进行此类操作,它也会存储在用户端,而不是您的服务器。 If your task is indeed to write something on user end, please refer to localStorage documentation (or couple other API implementations).如果您的任务确实是在用户端写一些东西,请参考localStorage 文档(或其他 API 实现)。 But if you are going to user that file for some purpose outside of your browser, it will be non-trivial task.但是,如果您要在浏览器之外出于某种目的使用该文件,这将是一项重要的任务。

If you want to save file on server, then you need to hande it on back end.如果要将文件保存在服务器上,则需要将其交给后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM