我尝试向我的Mlab数据库发送HTTP POST请求。
在开发中,一切都很好。但在生产环境中,我的请求发送时没有任何值。
我使用MongoDB + Express + Reactjs + Node - MERN堆栈-+ Heroku PaaS。
此外,在我的会议开始时,我收到了一条关于以下事实的消息:
Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script
但我现在不再收到这条消息:我已经设置了我的服务工作者在每次重新加载时更新,并在我的HTML文件夹中移动了一份我的serviceworker.js。
为什么当我发送HTTP POST请求时,它们仍然是空的?
关于我的日志,我的服务人员和file.map似乎有问题:
2018-05-24T18:25:39.795372+00:00 herokurouter路由器: at=info method=GET
path="/static/js/main.3685cd33.js.map“host=#.com
request_id=03674121-2fac-42ec-9459-e5fa17eb2ea7 fwd="93.27.27.184“dyno=web.1 connect=0ms service=7ms status=404 bytes=413 protocol=https 2018-05-24T18:25:41.576623+00:00 service路由器: at=info method=GET path="/service- worker.js”host=#.herokuapp.com request_id=065fb0eb-6606-4603- 8673-75e59445a1ea fwd="93.27.27.184“dyno=web.1 connect=0ms service=1ms status=404 bytes=400 2018-05-24T18:25:41.902150+00:00 service路由器:path="/static/css/main.969d7734.css.map“host=#.herokuapp.com request_id=a16b6a78-9167-4672-9888-3f141a46c3ef fwd="93.27.27.18
4“dyno=web.1 connect=0ms service=1ms status=404 bytes=415 protocol=https Here my App.js:
(...)
alert("word = "+ this.state.word) // **SO FAR, THE VALUE IS STILL DISPLAYED**
fetch("/api/words",{
method:"POST",
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type':'application/json'
},
body:JSON.stringify({ // **THEN I LOST THE CONTACT WITH THE VALUES FROM HERE**
"word":this.state.word,
"definition":this.state.definition,
"sound":this.state.audioRecorded
})
})
.then((res) => res.json())
.then((data) => console.log(data))
}
<form>
(...)
</form>
)
}
};
这是我的server.js:
const {createServer} = require("http");
var express = require("express")
var path = require("path")
var app = express() ;
var port = process.env.PORT || 7500 ;
var databaseName = "ntsat";
var bodyParser = require("body-parser");
app.use(bodyParser.json()) ;
app.use(express.static(path.join(__dirname, '../react-ui/public/index.html')));
var mongoose = require("mongoose");
var url = process.env.MONGOLAB_URI;
mongoose.connect([url to my mlab])
var db = mongoose.connection;
db.on ("error", console.error.bind(console, "connection fails"))
db.once("open", function () {
console.log(`mongoose : connection succeed on ${databaseName} database`);
});
app.use("/api", require("./router/router.js"));
app.listen( port, function(){
console.log("Express server listening on port %d in %s mode", this.address().port,
app.settings.env);
});
module.exports = app;
发布于 2018-06-01 05:18:57
我已经更改了Axios module的Fetch模块,现在可以正常工作了。
https://stackoverflow.com/questions/50517799
复制相似问题