我已经用app.post和app.get设置了一个node.js服务器,我想将用户填写的表单值中的数据发布到phpmyadmin中,以便用户将其插入数据库中。我为节点服务器使用Express,body-parser,mysql。现在,我在我的Registration类中使用提取,我认为该错误来自此。错误是这样的:TypeError:网络请求失败。
我使用通过电缆连接到我的PC的Android移动设备。我检查/尝试过的内容:
async handleRegistration(){
console.log(this.state.name, this.state.email, this.state.password);
await fetch('http://my_ip:3000/newCustomer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"name": this.state.name,
"email": this.state.email,
"password": this.state.password
})
})
.then(response => response.json())
.then(responseJson => console.log(responseJson))
.catch((error) =>{
console.warn(error);
});
}
这是我的提取代码。我在Routes.js(节点服务器)中定义了uri newCustomer,与端口3000相同。
我希望取回能够正常工作,并且数据可以发布。至少,不要给出此错误。我得到的是以下我一直在努力的错误消息:TypeError:网络请求失败。我想念什么吗?
答案 0 :(得分:1)
网络错误指向您的第二点,I use my ip4 address (not localhost)
。
在使用移动设备并使用服务器的内部ip (来自ipconfig
)时,移动设备和服务器 必须位于服务器上。同一网络 。为了使移动设备可以访问服务器IP地址,简单地说,两者都需要连接到同一路由器。
但是,如果您不在同一个网络中,则可能需要转发服务器ip:port并使用公共IP(Google什么是我的IP)而非内部私有IP从移动设备访问服务器。
答案 1 :(得分:0)
您应该使用async / await或promise链,但不能同时使用两者。尝试这样的事情:
Datum window countersOUT countersIN RESULT CalResult
2018-01-04 08:30:00.000 08:30 0 0 0 0
2018-01-04 09:00:00.000 09:00 2 1 1 1
2018-01-04 09:30:00.000 09:30 1 0 2 2
2018-01-04 10:00:00.000 10:00 25 9 18 18
2018-01-04 10:30:00.000 10:30 45 41 22 22
2018-01-04 11:00:00.000 11:00 38 37 23 23
2018-01-04 11:30:00.000 11:30 50 51 22 22
2018-01-04 12:00:00.000 12:00 21 24 19 19
2018-01-04 12:30:00.000 12:30 12 19 12 12
2018-01-04 13:00:00.000 13:00 25 18 19 19
2018-01-04 13:30:00.000 13:30 35 27 27 27
2018-01-04 14:00:00.000 14:00 81 9 52 52
2018-01-04 14:30:00.000 14:30 113 18 70 70
2018-01-04 15:00:00.000 15:00 116 34 71 71
2018-01-04 15:30:00.000 15:30 123 36 54 54
2018-01-04 16:00:00.000 16:00 127 35 50 50
2018-01-04 16:30:00.000 16:30 103 19 47 47
2018-01-04 17:00:00.000 17:00 79 31 27 27
2018-01-04 17:30:00.000 17:30 50 16 26 26
2018-01-04 18:00:00.000 18:00 28 11 17 17
2018-01-04 18:30:00.000 18:30 16 15 2 2
2018-01-04 19:00:00.000 19:00 0 2 0 0
2018-01-04 19:30:00.000 19:30 0 0 0 0
2018-01-04 20:00:00.000 20:00 0 0 0 0
2018-01-04 20:30:00.000 20:30 0 0 0 0
2018-01-04 21:00:00.000 21:00 0 0 0 0
2018-01-04 21:30:00.000 21:30 0 0 0 0
2018-01-04 22:00:00.000 22:00 0 0 0 0
答案 2 :(得分:0)
就我而言,它与服务器证书有关。
我正在运行一个 node.js https 服务器,它是从 Web 浏览器和邮递员通过 POST 方法访问的。但是 React Native android 应用上的 fetch 方法给出了 TypeError: Network request failed 这是非常不明智的。
做了这么多工作,我发现我的证书有问题。我正在使用让我们加密证书并使用 win-acme 工具生成。它为 node.js 等服务器生成 .pem 文件。我正在使用 key.pem (make settings.json>PrivateKeyExportable:true) 和 crt.pem 导致错误,我用 chain.pem 文件更改了 crt.pem。然后 işte bu* 它起作用了。
** 土耳其语的意思就是这样。
这里是 node.js 服务器的代码:
const https = require("https"),
fs = require("fs");
const options = {
key: fs.readFileSync('siteadresi.com-key.pem', 'utf8'),
cert: fs.readFileSync('siteadresi.com-chain.pem', 'utf8') //must be chain.pem
};
const express = require('express')
const qs=require('qs')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
https.createServer(options, app).listen(8080); // allow 8080 port on firewall