【问题标题】:JSON parse error: unrecognised token <JSON解析错误:无法识别的令牌<
【发布时间】:2017-03-25 05:41:12
【问题描述】:

我正在尝试在我的应用中实现用户注册。我使用 node.js express 创建了我的后端,并在我在路由器文件夹中创建的 .js 文件中传递了一个插入查询。我也在 app.js 中添加了所有必要的东西。

以下是我的后端代码。

var express = require('express');
var router = express.Router();
var mysql =  require('mysql');

var connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database:'MobileDB',
})

/* GET users listing. */
router.post('/', function(req, res, next) {

        var name = req.body.name;
        var email = req.body.username;
        var password = req.body.password;
        var phone = req.body.phone;
        connection.query("INSERT INTO `user` (`id`, `name`, `username`, `password`, `phone`) VALUES (NULL, '', '', '', '')",[null,name,username,password,phone],function(err,row,fields){
          if (err) console.log(err);

          if(row.length > 0){
              res.send({'success': true, 'message': 'welcome new user'});
          } else {
            res.send({'success':false,'message':'user not found, please try again'});
          }
      });
  });

module.exports = router;

以下是我的注册页面:

import React, { Component } from 'react';
import{
  Image,
  View
} from 'react-native';
import { Container, Content, List,Thumbnail, ListItem, InputGroup, Input, Icon, Text, Picker, Button,Header,Footer} from 'native-base';
import {
Router,
Actions,
Scene } from 'react-native-router-flux';
const Item = Picker.Item;
export default class Reg extends Component {
    constructor(props) {
        super(props);
        this.state = {
            name:'',
            username:'',
            password:'',
            phone:'',
            }

        };
    }
      Reg = () => {


            fetch('http://192.168.0.20:3000/reg', {
             method : 'POST',
                  headers: {
                  'Accept': 'application/json',
                  'Content-type': 'application/json',
                  },

                  body: JSON.stringify({
                  name:this.state.name,
                  username:this.state.username,
                  password:this.state.password,
                  phone:this.state.phone,

                  })
            })

            .then((response) => response.json())
            .then((res) => {
                  if(res.success=== true){
                  var username = res.message;

                     AsyncStorage.setItem('username', username);
                     Actions.Ideas();}
                     else {
                    alert(res.message);
                }



            })

            .done();

      }
    render(){
      return(
      <Container>
            <Content>
                    <View style={{alignSelf:'center'}}>
                    <Button transparent onPress={Actions.upload}>
                    <Icon name='ios-contact' style={{fontSize:30}}/>
                    </Button>
                    </View>
                    <View style={{top:20}}>
                    <List>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-person-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(name) => this.setState({name})}
                                  value={this.state.name} placeholder="Name" />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-at-outline" style={{color:'#5bc0de'}}/>
                                <Input onChangeText={(username) => this.setState({username})}
                                   value={this.state.username} placeholder="Email" />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-lock-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(password) => this.setState({password})}
                                 value={this.state.password}placeholder="Password" secureTextEntry />
                            </InputGroup>
                        </ListItem>
                        <ListItem>
                            <InputGroup>
                                <Icon name="ios-call-outline" style={{ color: '#5bc0de' }} />
                                <Input onChangeText={(phone) => this.setState({phone})}
                                 value={this.state.phone}placeholder="Phone" keyboardType="numeric" />
                            </InputGroup>
                        </ListItem>

                    </List>
                    <Button info style={{ alignSelf: 'center', marginTop:20, marginBottom: 20 }} onPress={this.Reg}>
                        SIGN UP
                    </Button>
                    </View>
                </Content>
            </Container>

        );
    }
}

当我单击注册按钮时,它显示无法识别的令牌错误。我不知道我哪里出错了。请帮忙。

【问题讨论】:

  • 从承诺中打印“响应”
  • 我不明白。
  • 192.168.0.20:3000这个IP是你的本地机器IP还是地球IP?
  • @ArunkumarRamaraj 本地机器IP
  • 登录效果很好。但我不知道为什么它不适用于注册。

标签: javascript json node.js express react-native


【解决方案1】:

附加 Chrome 调试器并启用异常暂停,您将了解发生了什么问题。

【讨论】:

    【解决方案2】:

    在你的 Reg() 函数中,'http://192.168.0.20:3000/reg' 应该是 'http://192.168.0.20:3000/' 我相信。

    我认为您正在使用当前代码返回一些不需要的 HTML。我认为是因为您试图向未定义的路由发送 POST 请求。

    就在您的 if 语句之前,console.log 响应 res,这样您就可以确切地知道您从后端返回了什么。

    【讨论】:

    • 我什么也没得到。当控制台.log。它直接进入错误屏幕。
    • 这可能没有什么区别,但在请求的标头部分中将 Content-Type 中的 T 大写。
    • 您的路线也被定义为/ 而不是/reg
    • 你知道如何使用chrome工具远程调试吗?
    • 您使用什么来运行应用程序?码?安卓工作室?
    猜你喜欢
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多