[转]Angular4 数据请求 POST、GET
本文转自:https://blog.csdn.net/dailuwen/article/details/79375980
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dailuwen/article/details/79375980
创建项目
ng new OBJECT_NAME
创建一个名为 httpRequest 的服务
ng generate service httpRequest
在app.module.ts 里面添加
providers : [HttpRequestService]
HttpRequestService.ts 文件
import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class HttpRequestService {
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json;application/x-www-form-urlencodeed; charset=utf-8'})
};
constructor(private httpClient : HttpClient) { }
httpPost(reqUrl : string, reqBody, comp, flag) {
//后台接收数据 需要 @RequestBody 标签
this.httpClient.post(reqUrl, reqBody, this.httpOptions)
.subscribe(
val => {
console.log('post请求成功', val);
if(val['success']){
comp.postOk(val, flag);
}else{
comp.postErr(val, flag);
}
},
error => {
console.log('post请求失败', error);
comp.postErr(error, flag);
}
);
}
httpGet(reqUrl, comp, flag){
this.httpClient.get(reqUrl, this.httpOptions)
.subscribe(
val => {
console.log('get请求成功', val);
if(val['success']){
comp.getOk(val, flag);
}else{
comp.getErr(val, flag);
}
},
error => {
console.log('get请求失败', error);
comp.getErr(error, flag);
}
);
}
show() : string {
return '我是 HttpRequestService 调用我干嘛';
}
}
创建一个名为 sku-from 的组件
ng g component sku-from
sku-form.component.html 文件
<div>
sku-form works!
<form #skuForm="ngForm" (ngSubmit)="onSubmit(skuForm)">
<div class="form-group">
<label for="name">货品代码</label>
<input type="text" class="form-control" [(ngModel)]="skuInfo.sku" name="sku" required minlength="4" />
</div>
<div class="form-group">
<label for="alterEgo">货品描述</label>
<input type="text" class="form-control" [(ngModel)]="skuInfo.descr" name="descr" required />
</div>
<br/>
<button type="button" nz-button [nzType]="'dashed'" (click)="newSku()">new sku</button>
<button type="submit" nz-button [nzType]="'primary'">Submit</button>
</form>
</div>
sku-form.component.ts文件
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import {HttpRequestService} from '../httpRequest.service'
import {Sku} from './../sku';
@Component({
selector: 'app-sku-form',
templateUrl: './sku-form.component.html',
styleUrls: ['./sku-form.component.css']
})
export class SkuFormComponent implements OnInit {
private api_sku_save = 'http://localhost:8081/sino-web/bas/sku/saveDetails';
private skuInfo : Sku = new Sku(1, '0000000000010', '红牛', new Date());
private skuForm : FormGroup;
reqsuccess : boolean;
reqsuccessMsg : string;
constructor(private httpRequestService : HttpRequestService) {
this.createForm();
}
ngOnInit() { }
createForm(){
this.skuForm = new FormGroup({
sku : new FormControl(this.skuInfo.sku, Validators.minLength(7)),
descr : new FormControl(this.skuInfo.descr, Validators.required)
});
}
newSku(){
this.skuInfo = new Sku(null, '', '', null);
}
onSubmit(formData) {
/**
* valid:是否有效、 invalid:无效、dirty:脏\status:状态、errors:显示错误
*/
if(formData.form.valid){
this.httpRequestService.httpPost(this.api_sku_save, this.skuInfo, this, 'save');
}
}
postOk (val, flag){
this.reqsuccess = true;
this.reqsuccessMsg = '';
}
postErr (val, flag){
this.reqsuccess = false;
this.reqsuccessMsg = val['msg'];
console.log(val);
}
}
创建一个名为 sku的类
ng g generate class Sku
sku.ts文件
export class Sku {
constructor(
public id : number,
public sku : string,
public descr : string,
public createdAt : Date
){}
}
---------------------
作者:戴子
来源:CSDN
原文:https://blog.csdn.net/dailuwen/article/details/79375980
版权声明:本文为博主原创文章,转载请附上博文链接!
[转]Angular4 数据请求 POST、GET的更多相关文章
- Ajax --- 数据请求
下面主要介绍(JS原生)数据请求的主要步骤: Ajax 数据请求步骤: 1.创建XMLHttpRequest对象 2.准备数据发送 3.执行发送 4.指定回掉函数 第一步:创建XMLHttpReque ...
- 携带cookie进行数据请求
前端进行数据请求有:普通的ajax(json)请求,jsop跨域请求,cors跨域请求,fetch请求...PC端这些请求方式中,普通的ajax(json)请求和jsop跨域请求是默认携带cookie ...
- XML 数据请求与JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- 使用 AFNetworking 进行 XML 和 JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- iOS - NetRequest 网络数据请求
1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...
- iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)
NSURL进行数据请求(POST与GET) 使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以 ...
- ASP.NET问题处理---“数据请求超时错误“”
数据请求超时,一般有2中解决方式: 1.页面AJAX处理数据时延长时间: 2.后台数据库连接取数据时延长时间. 由于我的后台数据库连接取数据为循环读取数据,所以不存在超时问题,这里具体说说如何修改AJ ...
- VueJS搭建简单后台管理系统框架 (二) 模拟Ajax数据请求
开发过程中,免不了需要前台与后台的交互,大部分的交互都是通过Ajax请求来完成,在服务端未完成开发时,前端需要有一个可以模拟Ajax请求的服务器. 在NodeJs环境下,通过配置express可访问的 ...
- 微信小程序数据请求方法wx.request小测试
微信小程序数据请求方法 wx.request wxml文件: <view> <textarea value="{{textdata}}"/> </vi ...
随机推荐
- 自我理解node.js相比java的优势
今天学习node.js,相比于之前学习过的java,node.js有一些优越之处.原因是它是一个基于Chrome v8引擎建立的JavaScript运行平台. (1)创建服务器:自行服务器来监听客户端 ...
- Apache Tomcat Eclipse Integration
An Illustrated Quick Start Guide Apache Tomcat makes hosting your applications easy. The Eclipse IDE ...
- Codeforces831A Unimodal Array
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 因为曾经装过Mysql导致再次装时windows无法启动MySQL服务报错1067的解决方法
找到这里 MySQL右击属性 检查这里的可执行文件的路径是否正确,因为我这里显示的是原先的文件夹所以会一直启动失败,修改一下 这里你去百度经验 windows服务修改可执行文件路径 网址https:/ ...
- PowerShell实现简单的搜索地区功能
Add-Type -AssemblyName Microsoft.VisualBasic $VBI = [Microsoft.VisualBasic.Interaction] function Sea ...
- git安装以及初始化
安装文档参见:https://www.cnblogs.com/ximiaomiao/p/7140456.html 注意:安装成功后,用cmd进行基本信息设置时,当出现“git不是内部或外部命令,也不是 ...
- 《HTTP权威指南》3-HTTP报文
报文流 HTTP报文是在HTTP应用程序之间发送的数据块,这些数据块以文本形式的元信息开头,这些信息描述了报文的内容及含义,后面跟着可选的数据部分.这些报文在客户端,服务器和代理之间流动. 报文的组成 ...
- TeeChart For VCL/FMX V2017使用教程:第一章-准备开始
https://blog.csdn.net/vbfgm/article/details/79338775 第一章 准备开始-构建图表和填充数据序列 1.1 简介 通过代码或Dataset(数据集)访问 ...
- Idea环境下git 图形化操作
大家好,之前我们介绍了<IDEA环境下GIT操作浅析之一Idea下仓库初始化与文件提交涉及到的基本命令>和<IDEA环境下GIT操作浅析之二-idea下分支操作相关命令>,本文 ...
- java责任链模式及项目实际运用
1.前言 上次我们认识了java责任链模式的设计,那么接下来将给大家展示责任链模式项目中的实际运用.如何快速搭建责任链模式的项目中运用. 2.简单技术准备 我们要在项目中使用借助这样的几个知识的组合运 ...