node.js - 在我的反应应用程序中,POST 方法不起作用
问题描述
我是新来的反应,并创建了一个简单的反应应用程序。我的应用程序的屏幕截图如下:
在“添加到购物车”按钮中,我为 POST 方法实现了一个功能。但是,错误是每当我单击按钮时,控制台上都不会出现任何内容,我猜 POST 方法不起作用。在控制台上只看到一个空数组,如下所示:
我的 PDP.js 代码
import React, { Component } from "react";
import { Route, Link, BrowserRouter } from "react-router-dom";
import axios from "axios";
class PDP extends Component {
state = {
pdpCategory: []
};
fetchData = id => {
axios
.get(`http://localhost:3030/product/` + id)
.then(response => {
console.log(response.data.express.catalogEntryView);
this.setState({ pdpCategory: response.data.express.catalogEntryView });
})
.catch(err => {
console.log(err);
});
var data = new FormData();
const payload = {
id: this.props.match.params.uniqueID,
description: this.props.match.params.longDescription
};
data.append("myjsonkey", JSON.stringify(payload));
fetch('http://localhost:3030/posts', {
method: 'POST',
body: data
})
console.log(data)
};
componentDidUpdate(prevProps) {
let currentId = this.props.match.params.id;
let previousId = prevProps.match.params.id;
if (currentId !== previousId) {
this.fetchData(currentId);
}
}
componentDidMount() {
let { id } = this.props.match.params;
this.fetchData(id);
}
doSomething = function (e) {
alert('it works!');
e.preventDefault();
}
render() {
const { pdpCategory } = this.state;
console.log(pdpCategory);
const picUrl = "https://149.129.128.3:8443";
return (
<div>
<div className="container">
<div className="row">
<form onSubmit={this.doSomething}>
{pdpCategory &&
pdpCategory.map(pdpList => {
return (
<div key={pdpList.uniqueID} className="col-md-4">
<h2 key={pdpList.uniqueID} />
<img className="pdpImage " src={picUrl + pdpList.thumbnail} />
<p>
Price : {pdpList.price[0].value}{" "}
{pdpList.price[0].currency}
</p>
<p>
Description: {pdpList.longDescription}
</p>
<button type="submit">Add to Cart</button>
</div>
);
})}
</form>
</div>
</div>
</div>
);
}
}
export default PDP;
另外,我使用 node js 来获取 react 应用程序的 API。
server.js 代码
const express = require('express');
const cors = require('cors');
const Client = require('node-rest-client').Client;//import it here
const app = express();
app.use(cors());
app.post('/posts', (req, res) => {
var client = new Client();
// direct way
client.post("https://jsonplaceholder.typicode.com/posts", (data, response) => {
res.send({ express: data });
});
});
app.get('/topCategory', (req, res) => {
var client = new Client();
// direct way
client.get("http://149.129.128.3:3737/search/resources/store/1/categoryview/@top?depthAndLimit=-1,-1,-1,-1", (data, response) => {
res.send({ express: data });
});
});
app.get('/category/:id', (req, res) => {
var id = req.params.id;
console.log(req.params.id)
var client = new Client();
// direct way
client.get("http://149.129.128.3:3737/search/resources/store/1/productview/byCategory/"+id, (data, response) => {
res.send({ express: data });
});
});
app.get('/product/:id', (req, res) => {
var id = req.params.id;
console.log(req.params.id)
var client = new Client();
// direct way
client.get("http://149.129.128.3:3737/search/resources/store/1/productview/byId/"+id, (data, response) => {
res.send({ express: data });
});
});
const port = 3030;
app.listen(port, () => console.log(`Server running on port${port}`));
有人可以帮我解决我哪里出错了。是因为我在服务器中配置错误还是因为我的事件处理程序不起作用。如果有人让我对此有所了解,我将不胜感激。
解决方案
您的表格调用this.onSubmit
:
<form onSubmit={this.onSubmit}>
而且您的组件没有onSubmit
方法。
推荐阅读
- visual-studio-code - VSCode 仅针对打开的文件显示 TSLint 错误
- php - 从时间戳开始的碳计数分钟
- unity3d - 将 XNA Shader 通道、混合和剔除模式移植到 Unity shaderlab
- amazon-s3 - 是否可以使用 Azure 数据工厂 V2 从请求者付款存储桶中的 Amazon S3 对象复制数据?
- azure - 将 Azure Powershell 中的虚拟机名称拆分为多个部分
- javascript - Using unary plus (+) to compare Dates for equality
- azure - What is the role of feature type in AzureML?
- apache-spark - 使用 mapGroupState 的结构化流式处理导致 GC 和性能问题
- python-3.x - 将 .pb 转换为 .tflite ,RuntimeError: TOCO failed 请参阅控制台获取信息
- maven - 是否有理由(除了免费托管)将战争文件发布到 Maven 存储库?