【发布时间】:2013-02-09 03:17:37
【问题描述】:
我正在尝试获取我为节点应用程序中的 ajax 帖子发送的值。使用this post 作为指导,到目前为止我有这个:
在节点中:
var express = require('express');
var app = express();
var db = require('./db');
app.get('/sender', function(req, res) {
res.sendfile('public/send.html');
});
app.post('/send_save', function(req, res) {
console.log(req.body.id)
console.log(req.body.title);
console.log(req.body.content);
res.contentType('json');
res.send({ some: JSON.stringify({response:'json'}) });
});
app.listen(3000);
在 AJAX 方面:
$('#submit').click(function() {
alert('clicked')
console.log($('#guid').val())
console.log($('#page_title').val())
console.log($('#page-content').val())
$.ajax({
url: "/send_save",
type: "POST",
dataType: "json",
data: {
id: $('#guid').val(),
title: $('#page_title').val(),
content: $('#page-content').val()
},
contentType: "application/json",
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
console.log(data);
console.log('process sucess');
},
error: function() {
console.log('process error');
},
});
})
这个问题是我不能 req.body.id(以及任何其他值,如标题或内容),我在节点中收到此错误:
TypeError: Cannot read property 'id' of undefined
如果我将这些调用注释掉,则 ajax 是成功的。我迷路了。我是不是忘记了什么?
【问题讨论】:
-
我有同样的问题 - 尝试使用
param('postVariableName')
标签: javascript jquery ajax node.js express