arslan
arslan

Reputation: 55

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

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 component.

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?

Upvotes: 5

Views: 15973

Answers (3)

Samuel RIGAUD
Samuel RIGAUD

Reputation: 1702

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

Upvotes: 0

Vikram Saini
Vikram Saini

Reputation: 2771

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

Upvotes: 2

pirs
pirs

Reputation: 2463

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

Upvotes: 2

Related Questions