我试图在发出Axios POST请求后从Node / Express服务器接收响应。
我能够成功将消息发送到我的服务器,该服务器已在控制台中登录。但是,我希望能够在服务器端使用response.send()将一些数据返回到我的浏览器。
为什么axios.post.then()不在控制台中记录此响应?
-客户端-
<html>
<head>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<button onclick="axiosPost()">Post Test</button>
<script>
function axiosPost() {
axios.post('http://localhost:3000/submitMessage', {
message: "sample message",
}).then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
</script>
</div>
</body>
</html>
-服务器端-
const express = require('express');
const app = express();
const port = 3000;
var path = require('path');
// serves index.html
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'))
});
app.listen(port, () => console.log(`Listening at http://localhost:${port}`))
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.post('/submitMessage', function(request, response){
message = request.body.message;
console.log("Message: " + message);
response.send('Server response message!!');
})
答案 0 :(得分:1)
好吧,我从未使用过Axios,但我认为您需要返回JSON
app.post('/submitMessage', (request, response) => {
message = request.body.message;
console.log("Message: " + message);
response.status(200).json({
status: 200,
ok: true,
data: {
msg: message
// Any data for the response
}
})
})
您还可以在前端使用Async / Await
有一个不错的帖子here,您可以查看有关它的信息
答案 1 :(得分:0)
在前端收听response.end('Message')
。或在服务器端使用简单的# read input data -- search data for departure months (columns) across shopping days (rows)
df = pd.read_csv('test_arima_states_input.csv', header=0)
# format date columns
df['date'] = pd.to_datetime(df.date, format='%m/%d/%Y')
df['date'] = df['date'].dt.strftime('%m/%d/%Y')
state_data = df[['State', 'date', 'Jul-20']]
spark_df = spark.createDataFrame(state_data)
results = spark_df.groupby('State').apply(run_arima_fcst)
results.show()