admin管理员组

文章数量:1290234

I try to create signup page and send data to mysql db. I just wrote this codes.. server.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 this is my ponent.

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>

             )
    }}

I just try to send data to mysql db. I got this error message -->Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Why?What is wrong?

I try to create signup page and send data to mysql db. I just wrote this codes.. server.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 this is my ponent.

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>

             )
    }}

I just try to send data to mysql db. I got this error message -->Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Why?What is wrong?

Share Improve this question asked Jul 11, 2017 at 10:20 arslanarslan 552 gold badges2 silver badges6 bronze badges 1
  • You should really see the actual JSON, but my guess is that something goes wrong server-side and the server responds with an HTML error page (thus starting with a <) and not the JSON. Therefore you also need to check the server log files too. – Nikos Paraskevopoulos Commented Jul 11, 2017 at 10:22
Add a ment  | 

3 Answers 3

Reset to default 2

You want a response in Json (with fetch):

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

But you send a html (server.js):

res.end('Success');

Try to send a json instead:

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

May be your response body is html,try to parse it in jSON and also try to add headers as

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);
});

Ok I encounter this problem just by forgetting the proxy in my package.json. Be sure to check it.

本文标签: javascriptUncaught (in promise) SyntaxError Unexpected token lt in JSON at position 0Stack Overflow