簡體   English   中英

反應:錯誤TypeError:網絡請求失敗

[英]React : error TypeError: Network request failed

我已經將節點js用作僅響應一些Json數據的服務器。

 //https var privateKey = fs.readFileSync('./ssl/key.pem').toString(); var certificate = fs.readFileSync('./ssl/cert.pem').toString(); var credentials = {key: privateKey, cert: certificate}; var httpServer = http.createServer(app); var httpsServer = https.createServer(credentials, app); var PORT = 18080; var SSLPORT = 18081; httpServer.listen(PORT, function () { console.log('HTTP Server is running on: http://localhost:%s', PORT); }); httpsServer.listen(SSLPORT, function () { console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT); }); app.get('/listUsers', function (req, res) { res.header('Content-Type', 'application/json'); res.header('Access-Control-Allow-Origin','*'); res.send(JSON.stringify({ a: 1 ,'id':2,'title':3})) }); 

我的反應代碼如下:

 let http_url = "https://localhost:18081/listUsers"; fetch(http_url, init) .then((response) => response.json()) .then((responseJson) => { this.setState({message: `${responseJson['id']} - ${responseJson['title']}`}); }) .catch(e => {console.log(`error ${e}`)}); 

通過在Chrome中輸入“ https:// localhost:18081 / listUsers ”,我可以獲得真正的json響應結果,但是反應模擬器總是告訴我“反應:錯誤TypeError:網絡請求失敗”。 我認為原因可能是節點響應標頭不正確。 誰能給我一些建議? 非常感謝!

您是否更改過這樣的節點端代碼?

res.json({ a: 1 ,'id':2,'title':3});

我認為您正在將數據作為字符串傳遞。 要求是json

另外,如果獲得成功,請先嘗試使用http://,然后再嘗試使用https://。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM