我试图使用ajax将输入值app.js发送到我的节点服务器
突然我单击按钮时出现错误
这是我的html
<body>
<input type="text" name="username" id="username"/><br><br>
<input type="password" name="password"/><br><Br>
<button id="SubmitBtn" type="submit">Submit</button>
</body>
我的ajax
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$('#SubmitBtn').click(function (event) {
var username = document.getElementById('username').value;
$.ajax({
url: 'http://localhost:3000',
data:{username},
dataType: "jsonp",
jsonpCallback: "_testcb",
cache: false,
timeout: 5000,
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error connecting to the Node.js server... ' + textStatus + " " + errorThrown);
}
});
});
});
</script>
我的app.js
var http = require('http');
http.createServer(function (req, res) {
var username = req.body.username;
console.log(username);
//do whatever you want
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('_testcb(\'{"message": "Welcome! Node.js Responding!"}\')');
}).listen(80)
这是错误消息
连接到Node.js服务器时出错... parsererror语法错误:意外令牌'<'