1

I'm not sure my title is the good explain of my problem so i will try to explain here.

I have a springBoot application on my server, and using angular for front.

I send a request to my server :

@PostMapping(value="/setup/rate/electricityToImport")
public void updateNewElectricityRate( HttpServletRequest request) throws IOException, SAXException, ParseException {
    String rate = request.getParameter("electricityToImport");      

    system.out.println(rate);

    //here i will save rate in file.

}
var rate = this.myGridImport.getselectedcell()['value'];
console.log(rate)

$.ajax({
  url: "/api/setup/rate/electricityToImport",
  type: 'POST',
  dataType: "text",
  data: 'electricityToImport=' + rate
})

Now i want to know if the file is saved and display "File Saved" on the front (angular application) but how can i send to my front that save is finished well

Thanks a lot for your help! Math

Phil
  • 157,677
  • 23
  • 242
  • 245
Mathieu d.o
  • 111
  • 6
  • Are you saying that the file is on the client and you want to send it using a web browser and then save in on a server? – Scary Wombat Jan 16 '19 at 01:46
  • not exactly, i modify the file which is on my server with my web application, after modify, i send modification to my server which save the news values. After save, i want to tell me to my client that the save of my file is finished well – Mathieu d.o Jan 16 '19 at 01:52
  • You can use ajax onsuccess - see https://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success – Scary Wombat Jan 16 '19 at 01:57

2 Answers2

1

@mavriksc has already shown proper methods. Normally you can achieve that in two ways:

shared service

If you are using a shared global service to handle request, you can just handle the response error there (if no error, then you can just tell the clients saved successfully, right?).

It could be something like this:

getJson(): Observable<string> {
    return this.http
      .get(a_url_here)
      .pipe(
      map((res: any) => {
        console.log("Data got: ");
        console.log(res);
        return res;
      }),
      catchError(error => this.handleError<string>(error, "Network Error!"))
    );
  }

standalone request

If you are requesting in your component directly or handle the error there, you can achieve that along with subscribe() as this:

this.getYourData(your_url).subscribe(jsonStr => this.testContent = jsonStr, 
                              error => this.handleError<string>(error, "Network Error!"));
Hearen
  • 7,420
  • 4
  • 53
  • 63
0

web frameworks will return an error code if you throw an exception from your controller, otherwise return 200OK . so in your angular front end your client will have some Observable as a response to this request. in complete you can display file saved. and in error you can display some info about that.

mavriksc
  • 1,130
  • 1
  • 7
  • 10