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

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

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

那是你的服务器:

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



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5477137.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存