gpt4 book ai didi

javascript - JSON解析错误: unrecognised token <

转载 作者:行者123 更新时间:2023-12-03 04:42:48 31 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现用户注册。我使用 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>

);
}
}

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

最佳答案

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

关于javascript - JSON解析错误: unrecognised token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43012986/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com