HttpClient.post成功,但未将数据发送到json

时间:2019-06-04 08:10:13

标签: json angular angular-httpclient

我正在尝试将我的角度应用程序中的数据存储到JSON中。 JSON存储在我的应用程序的assets文件夹中,并且我在服务器上安装了该应用程序。

我正在使用HttpClientPOST数据到JSON。它说成功了,但实际上并没有发送数据。

这是我的.ts:

import { HttpClient } from '@angular/common/http'
import { HttpHeaders } from '@angular/common/http'

export class OpscreenComponent implements OnInit {

  constructor(private httpService: HttpClient ) { }

  ngOnInit() {
    var jsonpost = {
      "testing": [
        {
          "anothertest":"here",
          "anumber": 1
        }
      ]
    }
    var headers = new HttpHeaders({
      'Content-Type': 'application/json'
    })
    var options = { headers: headers }
    this.httpService.post("http://servername/angularApp/assets/testing.json", jsonpost, options)
    .subscribe(
      data=> {
        console.log("POST Request is Successful ", data )
      },
      error => {
        console.log("Error ", error)
      }
    )

  }

}

我没有收到任何错误消息,并且请求成功,因为它正在控制台中记录POST Request is Successful null

JSON为空白,启动成功,并且POST成功后仍为空白(因此控制台中的null。)

为什么不将数据发布到JSON?

我将httpClient用于get,所以我知道它已正确导入。

1 个答案:

答案 0 :(得分:-2)

您要尝试的是将文件写入磁盘,但是,Angular的HttpClient无法将文件写入磁盘。至少没有经典的实现。

您可能更喜欢添加一个可以写入磁盘的服务器,并且您的HttpClient将使用相应的数据访问该服务器。

相关问题