Tuesday 14 March 2017

axios method 'POST' error 'Error: Network Error'

i'm beginner

i try make web with react (react-redux). i use 'axios' to connect api server but error see event test.

test method 'GET' is ok.

test method 'POST' is error to browser(firefox), 'Error: Network Error'

client code

export const isLogin = (user) => {
  return axios.post('http://127.0.0.1:3001/login', user)
    .then(res => {
      store.dispatch(toLogin(res.data));
      return res;
    });
}

api server with express

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var auth = './auth';


app.set('port', (process.env.PORT || 3001));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(function(req,res, next){
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Cache-Control', 'no-cache');
  next();
})

app.all('/', function(req,res){
  res.send('Hi this is API for web')
})

app.post('/login', function(req,res){
  res.send('Post request');
});

app.listen(app.get('port'), function(){
  console.log('>> start web api');
  console.log('>> data listen port : ' + app.get('port'));
})

Are there any settings that are faulty?

Thanks for the help.



via wit_peter

No comments:

Post a Comment