drrog9853 2016-08-21 06:32
浏览 48

为什么我在与节点做出反应时会得到404

I am trying to send the data inserted into form to server via ajax request like this:

 handleUserSubmit: function(user) {
   var users = this.state.data;
   user.id = Date.now();
   var newUsers = users.concat([user]);
   this.setState({data: newUsers});
   $.ajax({
     url: this.props.url,
     dataType: 'json',
     type: 'POST',
     data: user,
     success: function(data) {
       this.setState({data: data});
     }.bind(this),
     error: function(xhr, status, err) {
       this.setState({data: users});
       console.error(this.props.url, status, err.toString());
     }.bind(this)
   });
 },

and the rendering is done as:

ReactDOM.render(
  <UserBox url="../users" pollInterval={2000} />,
  document.getElementById('data')
);

I am getting this weird console error:

POST http://localhost:3000/users 404 (Not Found)

Since the "users" file (assuming it's trying to access users.json file is in a directory up one level). I have tried all variation possible : "./users", "../users", "#/users" etc...

The code error on this line:

url: this.props.url,

in the code given above. Any idea what's going on?

My node.js code is:

app.post('/users', function(req, res) {
  fs.readFile(USERDATA, function(err, data) {
    if (err) {
      console.error(err);
      process.exit(1);
    }
    var users = JSON.parse(data);
    var newUser = {
      id: Date.now(),
      firstname: req.body.firstname,
      lastname: req.body.lastname,
      address: req.body.address,
      phonenumber: req.body.phonenumber,
      email: req.body.email,
      licensestate: req.body.licensestate,
      licensenum: req.body.licensenum
    };
    console.log(newUser)
    users.push(newUser);
    fs.writeFile(USERDATA, JSON.stringify(users, null, 10),              

function(err) {
      if (err) {
        console.error(err);
        process.exit(1);
      }
      res.json(users);
        });
      });
     }); 
  • 写回答

1条回答 默认 最新

  • donglu953744 2016-08-21 18:15
    关注

    Refresh the state after updating the server

    评论

报告相同问题?

悬赏问题

  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile