【问题标题】:Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
【发布时间】:2017-12-15 08:27:45
【问题描述】:

我尝试创建注册页面并将数据发送到 mysql db。我刚刚写了这个代码.. 服务器.js

  var mysql = require('mysql');
    var http = require('http');
    var port = process.env.PORT || 1337;




    var con = mysql.createConnection({
        host: "localhost",
        user: "*****",
        password: "*******",
        database: "node"
    });

    con.connect(function (error) {
        app.post('/', function (req, res) {

            var user = req.body;

            var query = connection.query('INSERT INTO signup(FirstName, LastName, Email,Password, PasswordConfirm) SET ?', user, function (err, result) {

            });
            res.end('Success');
        });
    });

app.js 这是我的组件。

class Signup extends React.Component {


    constructor() {
        super();
        this.state = { user: {} };
        this.onSubmit = this.handleSubmit.bind(this);
    }
    handleSubmit(e) {
        e.preventDefault();
        var self = this;

        fetch('/', { 
            method: 'POST',
            data: {
                firstname: self.refs.FirstName,
                lastname: self.refs.LasttName,
                email: self.refs.Email,
                password: self.refs.Password,
                passwordconfirm: self.refs.PasswordConfirm

            }
        })
          .then(function(response) {
              return response.json()
          }).then(function(body) {
              console.log(body);
          });
    }


    render(){
        return(
            <div>


        <form id="signup" onSubmit={this.onSubmit}>
            <input type="text" id="firstName" placeholder="FirstName" ref="firstname"/>
            <input type="text" id="lastName"  placeholder="LastName" ref="lastname"/>
            <input type="email" id="email" placeholder="Email" ref="email"/>    
            <input type="password" id="password" placeholder="Password" ref="password" />
            <input type="password" id="confirm" placeholder="PasswordConfirm"ref="passwordconfirm" />
            <input type="submit" />

        </form>
        </div>

             )
    }}

我只是尝试将数据发送到 mysql db。 我收到此错误消息 -->Uncaught (in promise) SyntaxError: Unexpected token

为什么?怎么了?

【问题讨论】:

  • 您应该真正看到实际的 JSON,但我的猜测是服务器端出了点问题,服务器响应 HTML 错误页面(因此以 &lt; 开头)而不是 JSON。因此,您还需要检查服务器日志文件。

标签: javascript mysql database reactjs


【解决方案1】:

您想要 Json 中的响应(使用 fetch):

console.log(response); // see the response before
return response.json();

但是你发送一个html(server.js):

res.end('Success');

尝试发送 json:

res.json({insert: "success"})

【讨论】:

  • 您的 server.js 中可能有错误?尝试使用console.log(response)查看它
【解决方案2】:

可能您的响应正文是 html,尝试在 jSON 中对其进行解析并尝试将标头添加为

fetch('/', { 
  method: 'POST',
  data: {
    firstname: self.refs.FirstName,
    lastname: self.refs.LasttName,
    email: self.refs.Email,
    password: self.refs.Password,
    passwordconfirm: self.refs.PasswordConfirm
  },
  headers: { 
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
})
.then(function(response) {
  console.log(response);
  return response.json();
})
.then(function(body) {
  console.log(body);
});

【讨论】:

  • 检查 Chrome 开发工具中的“网络”选项卡以查看服务器响应的内容
  • 我收到了这些错误 -->Uncaught TypeError: (intermediate value)(intermediate value).then 不是 Object.ReactErrorUtils.invokeGuardedCallback ( app2.js:5516) 在 executeDispatch (app2.js:5040) 在 Object.executeDispatchesInOrder (app2.js:5063) 在 executeDispatchesAndRelease (app2.js:2963) 在 executeDispatchesAndReleaseTopLevel (app2.js:2974) 在 Array.forEach () 在 forEachAccumulated (app2.js:8634) 在 Object.processEventQueue (app2.js:3177) 在 runEventQueueInBatch (app2.js:16988)
  • 尝试在函数句柄提交的末尾添加分号
  • 提交后,它重新加载页面,我的数据库中没有任何内容
  • 不,没有错误,但我没有看到我从页面发送的数据。它不在我的数据库表中
【解决方案3】:

好的,我只是因为忘记了 package.json 中的代理而遇到了这个问题。一定要检查一下。

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 2020-03-26
    • 2019-08-14
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2021-12-21
    相关资源
    最近更新 更多