Post and get data from server Angu
import { HttpClient, HttpHeaders,HttpParams,HttpClientModule} from '@angular/common/http';
handlePlayClick() {
this.play_clicked = true;
const params = new HttpParams()
.set('choice', this.select)
this.http.post(this.serverUrl,params)
.subscribe(
(val) => {
console.log("POST call successful value returned in body",
val);
this.result = val
if (val.result === 'win')
this.personal_score +=1
else if (val.result === 'lose')
this.computer_score +=1
this.total_played +=1
},
response => {
console.log("POST call in error", response);
},
() => {
console.log("The POST observable is now completed.");
});
this.select = '';
}
import HttpClientModule after BrowserModule
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],
declarations: [
AppComponent,
],
bootstrap: [ AppComponent ]
})
localserver need to add below code for localserver access
//for localhost access
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
请登录之后再进行评论