栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从角度服务器向节点服务器发出发布请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从角度服务器向节点服务器发出发布请求

那是你的服务器:

const express = require('express')const bodyParser = require('body-parser');const app = express()app.use(bodyParser.json());app.use(bodyParser.urlenpred({extended: true}) );app.all("/*", function(req, res, next){  res.header('Access-Control-Allow-Origin', '*');  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETe,OPTIONS');  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');  next();});app.post('/ping', function (req, res) {  res.send(req.body)})app.listen(3000, function () {  console.log('Example app listening on port 3000!')})

那是您的有角度的客户:

import { Component } from '@angular/core';import { HttpClient, HttpHeaders } from '@angular/common/http';@Component({  selector: 'app-root',  templateUrl: './app.component.html',  styleUrls: ['./app.component.css']})export class AppComponent {  user = { id : 1, name : 'Hello'};  constructor(private http: HttpClient) { }  callServer() {    const headers = new HttpHeaders()          .set('Authorization', 'my-auth-token')          .set('Content-Type', 'application/json');    this.http.post('http://127.0.0.1:3000/ping', JSON.stringify(this.user), {      headers: headers    })    .subscribe(data => {      console.log(data);    });  }}

回购https://github.com/kuncevic/angular-httpclient-
examples



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484997.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号