首页 > 解决方案 > 连接到 Node.js 服务器时出错... parsererror SyntaxError: Unexpected token '<'

问题描述

我试图将输入值发送到我的节点服务器 app.js 使用 ajax

当我单击按钮时突然出现错误

这是我的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>

我的阿贾克斯

<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 SyntaxError: Unexpected token '<'

标签: node.jsajax

解决方案


推荐阅读