hurriedly% 2015-11-10 10:04 采纳率: 100%
浏览 20

ReactJS ExpressJS-Ajax表单

Due to my starting level with React and Node/Express and, above all, time restriction, I'm developing a single page application without database, so I have only static pages.

I've configured react-router to have clear url, using the hash (http://localhost:8080/#/contacts) i avoid also server calls:

let history = createHistory({
  queryKey: false
});

I've the necessity to send mail from my form. To do this i've find nodemailer and i need to invoke it from an ajax call. Just to do some pratique with React i've configured my ajax call in my componentDidMount function:

openForm() {
    $('#contacts-form').on('submit', function(){
        $.ajax({
            url: '/mail',
            type: 'post',
            data: { msg: $('#data').val() },
            success: function(data){
                console.log(data);
            },
            error: function(){
                console.log('err form');
            }
        });
        return false;
    });
}

componentDidMount() {
    this.openForm();
}

Now, after I've installed body-parser and set my form's action to "/mail", in my server.js I've configured my .post route

app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
  extended: true
}));

app.post('/mail', function(req, res){
    res.send('ajax call recived');
});

So now once I try to submit my form, my ajax call fails, in console i get this error:

POST http://localhost:8080/mail 404 (Not Found)
err form

It's the first time i'm working with these tools, where i'm doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2024-五一综合模拟赛
    • ¥15 如何将下列的“无限压缩存储器”设计出来
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口