【发布时间】:2018-07-05 14:45:57
【问题描述】:
我有一个通过服务发布的 httpClient 帖子,它没有给我任何错误,但也没有将数据发布到数据库中。
代码如下:
dataService.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class DataService {
constructor(private http: HttpClient) {}
insertData() {
const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
return this.http.post('http://myurl/index.php', body, httpOptions);
}
}
app.component.ts
import { Component } from '@angular/core';
import { DataService } from './services/data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private dataService: DataService) {}
myInsert() {
if (this.dataService.insertData()) {
alert('Data Inserted Successfully');
}
}
}
最后是 app.component.html
<div (click)="myInsert()">Click Me</div>
我已经在 chrome 网络上查看了该帖子,但那里没有显示任何内容。 我该如何解决这个问题?
【问题讨论】:
标签: angular typescript angular5