[英]Using jQuery ajax with node.js to send POST request
我的myserver/index.js
有以下內容(與節點一起運行):
server.configure(function() {
server.use(express.methodOverride());
server.use(express.bodyParser());
server.use(server.router);
server.use(express.static(__dirname + '/public'));
server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
server.post('/signup/submit', function(req, res) {
console.log(req);
res.send(req.body);
});
server.listen(process.env.PORT || 12345, function() {
console.log('Server listening');
});
我的myserver/public/signup/index.html
文件的<script>
部分中包含以下內容
function submit() {
$.ajax({
url: "submit",
type: "POST",
contentType: "application/xml",
data: '<?xml version="1.0"?><user>John Smith</user>',
dataType: "xml",
success: function(data, textStatus, jqXHR) { alert(data); },
error: function(jqXHR, textStatus, errorThrown) { alert(errorThrown); }
});
調用commit()時,彈出警報,提示“錯誤:無效的XML:{}”。 從req
的控制台轉儲中,我看到該body
確實為空,但是進一步檢查顯示它已經收到了所有其他數據。 這是標題:
headers:
{ host: 'localhost:12345',
connection: 'keep-alive',
'content-length': '44',
origin: 'http://localhost:12345',
'x-requested-with': 'XMLHttpRequest',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11',
'content-type': 'application/xml',
accept: 'application/xml, text/xml, */*; q=0.01',
referer: 'http://localhost:12345/signup/',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'en-US,en;q=0.8',
'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' }
我是Ajax,jQuery和Node.js的新手,所以我不知道自己在做什么錯。 我覺得這是我缺少的東西。
謝謝。
嘗試這個:
$.ajax({
url: "submit",
type: "POST",
contentType: "application/xml",
data: "<user>John Smith</user>", // remove <?xml ...
dataType: "xml",
success: function(data, textStatus, jqXHR) { alert(data); },
error: function(jqXHR, textStatus, errorThrown) { alert(errorThrown); }
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.