Angular 5 – HttpClient Post没有发布
发布时间:2020-12-17 07:16:43 所属栏目:安全 来源:网络整理
导读:我有一个httpClient帖子,通过服务,没有给我任何错误,但它也没有将数据发布到数据库. 这是代码: dataService.ts import { Injectable } from '@angular/core';import { HttpClient,HttpHeaders } from '@angular/common/http';const httpOptions = { headers
我有一个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网络上的帖子,但没有显示任何内容. 解决方法
需要观察观察者提出请求.
myInsert() { this.dataService.insertData().subscribe( response => console.log(response),err => console.log(err) ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |