gpt4 book ai didi

javascript - Angular 发布表单数据(文本输入)到 Rest API

转载 作者:行者123 更新时间:2023-12-03 02:32:18 25 4
gpt4 key购买 nike

我正在尝试将数据(文本输入)从响应式(Reactive)表单发布到本地express.js 服务器上的REST API。姓名、电子邮件...只是基本的文本输入字段。

这里我将值从 Form 发送到服务 (Form.Component.ts)

  onSubmit(formDirective) 
{
this.personservice.senddata(this.personForm.value).subscribe(data=>{
console.log(data);
})
}

在服务中,我将数据发布到 REST API

constructor(private http: HttpClient) 
{
console.log('init PS')
}

getPeople(): Observable<People[]>
{
return this.http
.get<People[]>(this._peopleURL)
.map( (data: any) => data.people);
}

private _peopleURL = "http://localhost:8080/api/people";

senddata(data : any)
{
var body = JSON.stringify(data);
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post(this._peopleURL, data);
}

控制台日志显示正确的数据,但不会将数据发布到 REST API。 enter image description here

我错过了哪些步骤?

编辑:

Here is the code for my express.js server

const express = require('express');

const app = express();

const cors = require('cors')

var corsOptions = {
origin: 'http://localhost:4200',
optionsSuccessStatus: 200
}

app.use(function (req, res, next) {

res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

app.use(cors(corsOptions))

app.listen(8080, () => {
console.log('Server gestartet');
});


app.route('/api/people').get((req, res) => {
res.send({
people: [
{ vorname: 'max', nachname: 'müller', email: 'testmail@gmail.com', status: 'true', activity: 'Office' },
{ vorname: 'jeremy', nachname: 'püringer', email: 'jp@gmail.com', status: 'true', activity: 'Office' },
{ vorname: 'peter', nachname: 'schmidt', email: 'ps@bluwin.ch', status: 'false', activity: 'service' }
]
});
});

app.route('/api/people/:vorname').get((req, res) => {
const requestedPersonSurname = req.params['vorname'];
res.send({ vorname: requestedPersonSurname });
});

app.route('/api/save').get()


const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.route('/api/people').post((req, res) => {
res.send(201, req.body);
});

最佳答案

试试这个。

 senddata(data: any) {
var body = JSON.stringify(data);
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');;
return this.http.post(this._peopleURL, data);
}

Notice that we are building the HTTPHeaders object by chaining successive set() methods. This is because HTTPHeaders is immutable, and its API methods do not cause object mutation. Instead, a call to set will return a new HTTPHeaders object containing the new value properties.

<强> WORKING DEMO

关于javascript - Angular 发布表单数据(文本输入)到 Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com