Axiosを使用してフロントエンド(反応ネイティブ)からAPI(Node.JS)へのリクエストを作成しようとしています。しかし、私が得るエラーについては混乱しています。ここでは、エラーの印刷スタックトレースです:Nodejs + Reactネイティブ+ Axios + CORS
Network Error
- node_modules\axios\lib\core\createError.js:16:24 in createError
- node_modules\axios\lib\adapters\xhr.js:87:25 in handleError
- node_modules\event-target-shim\lib\event-target.js:172:43 in dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:554:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:387:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:182:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:302:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:116:26 in <unknown>
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:265:6 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:115:17 in callFunctionReturnFlushedQueue
だから私は、私はバックエンドとの通信を可能にするAxiosで何かを設定する必要がありますか、私はできるように私のバックエンドを設定しなければならないのかどうかわからないですフロントエンドのリクエストここでCORSとバックエンドのサーバーの私の設定です:他に
// server.js
var app = require('./app');
var routes = require('./routes');
var port = process.env.PORT || 3001;
var cors = require('cors');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var server = app.listen(port, function(){
console.log('Express server listening on port ' + port);
});
すべてが正常に動作します、私の方法は、GETとPOSTは、サーバ側で動作します。ここで
はAxiosと私の呼び出しです:
import axios from 'axios';
export function loginUser(parameters, onSuccess, onError) {
axios.post('http://localhost:3001/login', parameters)
// Succes :
.then((response) => {
console.log(response);
})
// Echec :
.catch((error) => {
console.log(error);
});
}
あなたはAxiosとの通話方法を共有できますか?そのエラーはCORSエラーのようには見えません。 – Corey
私はstackoverflowに新しいです、コメントにコードを入れる方法はわかりませんが、私は自分の投稿を更新しました、ごめんなさい – Citrix