weixin_33692284 2015-09-23 20:46 采纳率: 0%
浏览 21

POST上的Ajax发布404

I am trying to send a test email using node.

My server code index.js looks like this:

var http = require("http"),
    express = require('express'),
    nodemailer = require('nodemailer'),
     bodyParser = require('body-parser'),
    app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));

app.post('/contact', function (req, res) {

    var name = req.body.name;
    var email = req.body.email;
    var message = req.body.message;

    var mailOpts, smtpTrans;

    smtpTrans = nodemailer.createTransport('SMTP', {
        service: 'Gmail',
        auth: {
            user: "email@gmail.com",
            pass: "password"
        }
    });

    mailOpts = {
        from: name + ' <' + email + '>', //grab form data from the request body object
        to: 'cmatsoukis@gmail.com',
        subject: "Website Contact",
        text: message
    };

    smtpTrans.sendMail(mailOpts, function (error, response) {
        //Email not sent
        if (error) {
            res.send(false);
        }
            //Yay!! Email sent
        else {
            res.send(true);
        }
    });

});

app.listen(1337, '127.0.0.2');

This is my ajax code when I submit the form

var form = $("form#contact_form");
form.submit(function () {
    event.preventDefault();

    var name = $('#name').val();
    var email = $('#email').val();
    var msg = $('#message').val();

    var info = {"name" : name, "email": email, "message" : msg} 

    $.ajax({
        url: 'http://127.0.0.2:1337',
        type: 'POST',
        data: JSON.stringify(info),
        contentType: "application/json; charset=utf-8",
        jsonpCallback: 'callback', // this is not relevant to the POST anymore
        //dataType: 'json',
        success: function (data) {
            MailSuccess()
            lightSpeed();

        },
        error: function () {
            MailFail();
        }
    });
});

I am getting the error POST http://127.0.0.2:1337/ 404 (Not Found)

I do not think I have the url correct. I have app.post('/contact', function (req, res) { But I do not this that is correct. And shouldn't I put a file name in the url: 'http://127.0.0.2:1337',?

I believe I have everything else correct. Please let me know.

  • 写回答

1条回答 默认 最新

  • H_MZ 2015-09-23 20:48
    关注

    You've registered a POST handler for '/contact' but you are making the request to '/'.

    Since you haven't told express what to do for '/' it returns a Not Found.

    Make the two paths match.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型