Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a Json file in wwwroot/assets folder. I am using that file in the service to display data. I am trying to insert another record to that Json file. I have used the same url but when it posts the data from the service, it errors "failed to load the resource". I tried opening the url from the error and it opens the Json file and displays the values from the json file.

What I have tried:

Url for the service:
C#
private url = '/assets/accounts/account-details.json';


Service method:
C#
createService(param: AccountDetails): Observable<accountdetails> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let body = JSON.stringify(param);
    let options = new RequestOptions({ headers: headers });
    return this.http
      .post(this.url, body, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

Invoke from component:
C#
createAccountDetail() {
    let acd: AccountDetails[] = [{
      accountNumber: "23459501", tranDate: "09-Apr-2018", tranDetails: "ATM Withdraw", tranType: "Debit", tranAmount: 100.00, balance: 100
    }];
    let accountDetail = acd[0];

    this.accountService.createService(accountDetail)
      .subscribe(
      result => console.log(result),
      error => this.errorMessage = <any>error
      );   
}
Posted
Updated 10-Apr-18 8:58am
v2
Comments
Giridhari Mishra 10-Apr-18 12:56pm    
How to insert items into Json file using angular?
Kornfeld Eliyahu Peter 10-Apr-18 14:59pm    
It is confusing! It seems you try to post (that is HTTP POST) some content to an url of a file, and you except the content of that post to be written into that file?
Is it your first attempt on web development?
Giridhari Mishra 10-Apr-18 17:35pm    
I am using the HTTP GET to get the items from the json and display in the page. That is working fine.
return this.http.get(this.dataUrl)
      .map(response => response.json())
      .catch(error => Observable.throw(error.json()));


I was trying to insert item into the same json file. If that is possible pls let me know. This is not the first attempt on web development but first attempt to insert item to Json file using a service.
Kornfeld Eliyahu Peter 11-Apr-18 0:50am    
And that proves, that you do not understand web, how web servers handle HTTP verbs and what and why the differences...
Imagine that putting content on your web server was that easy as getting content... dream of any hacker...
Giridhari Mishra 11-Apr-18 11:19am    
Okay. I agree. I am not doing it as a live application or going to implement this in real project. I am just practicing this. I do not want to use a database and create service. Can you tell me how I can insert items into the Json file in angular using typescript? If you know and give me the details that will help else we can stop here and assume that is not possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900