大佬教程收集整理的这篇文章主要介绍了如何从角度服务器向节点服务器发出发布请求,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
那是你的服务器:
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.Json());
app.use(bodyParser.urlencoded({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
当我在Node服务器上打印请求的内容时,在任何地方都看不到用户数据。
这是我的节点服务器:
var http = require('http');
http.createServer( function (request,responsE) {
console.log(request);
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
这是Angular2代码:
import { Component,OnInit } from '@angular/core';
import { httpClient } from "@angular/common/http";
import { http,Response,Headers,requestOptions } from "@angular/http";
import 'rxjs/add/operator/retry'; // to be able to retry when error occurs
import { Observable } from "rxjs/Rx";
@Component({
SELEctor: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'Angular Test';
user = { id : 1,name : "Hello"};
constructor (private http: http) {}
ngOnInit(): void {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new requestOptions({ headers: headers });
console.log(thiS.User);
this.http.post("http://localhost:8080/",thiS.User,options)
.subscribe(
(err) => {
if(err) console.log(err);
console.log("success");
});
}
}
任何人都可以帮我或解释如何处理角度的http请求。
以上是大佬教程为你收集整理的如何从角度服务器向节点服务器发出发布请求全部内容,希望文章能够帮你解决如何从角度服务器向节点服务器发出发布请求所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。