正在获取SyntaxError:JSON中的意外令牌S在JSON.parse的位置0

时间:2019-07-05 05:35:27

标签: angular

我有以下方法存储我的数据,该数据返回“成功”作为响应。 我已成功将数据存储在数据库中,但仍然收到“ JSON在位置0处意外的令牌u”错误”错误。下面是我的模式。

   this.state={
  // This is our Default number value
  NumberHolder : 1,
  NumberHold : 1,
  sum: 0,
  }
  }

   GenerateRandomNumber = () => {
   let RandomNumber = Math.floor(Math.random() * 6) + 1;
   let RandomNumber1 = Math.floor(Math.random() * 6) + 1 ;
   let sum = RandomNumber + RandomNumber1;

    this.setState({
    NumberHolder : RandomNumber,
    NumberHold : RandomNumber1,
    sum
     });
     }

    getDice(diceNum) {
    let diceSource = require(`./images/dice1.png`)
    if (diceNum === 2) {
    diceSource = require(`./images/dice2.png`)
    } else if (diceNum === 3) {
    diceSource = require(`./images/dice3.png`)
    } else if (diceNum === 4) {
    diceSource = require(`./images/dice4.png`)
    } else if (diceNum === 5) {
    diceSource = require(`./images/dice5.png`)
    } else if (diceNum === 6) {
    diceSource = require(`./images/dice6.png`)
     } else {
    diceSource = require(`./images/dice1.png`)
    }
     return (
    <Image style={styles.dice}source={diceSource} />
     )
     }

     render() {
    // this is just for the demo purposes
   const imageSize = this.state.NumberHolder * 50


    return (
   <View style={styles.MainContainer} >


   <View style={{ position: 'absolute',
    top: 0,
    left: 20,
    width: 50,
    height: 50,
    marginRight:30,
    }}>
   <View style={{backgroundColor: '#006400'}}>
    <View style={{    position: 'absolute',
   top: 0,
   left: 20,
   width: 50,
   height: 50,
   marginLeft:40,

   backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
    'center',
   marginTop:10,
   color: '#ffffff',
     alignItems: 'center'}}>
          1
        </Text>
    </View>


    <View style={{    position: 'absolute',
    top: 0,
    left: 20,
    width: 50,
     height: 50,
     marginLeft:90,
    borderWidth: 1,
    borderColor: '#000000',
     backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
    'center',
    marginTop:10,
    color: '#ffffff',
     alignItems: 'center'}}>
          2
        </Text>
    </View>
    <View style={{    position: 'absolute',
    top: 0,
    left: 20,
    width: 50,
    height: 50,
    marginLeft:130,
    borderWidth: 1,
    borderColor: '#000000',

    backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
       'center',
    marginTop:10,
    color: '#ffffff',
     alignItems: 'center'}}>
          3
        </Text>
    </View>
    <View style={{    position: 'absolute',
     top: 0,
     left: 20,
      width: 50,
     height: 50,
     marginLeft:170,
     borderWidth: 1,
      borderColor: '#000000',
      backgroundColor: '#006400'}}>

     <Text style={{justifyContent: 'center',flex: 1,textAlign: 'center',
     marginTop:10,
     color: '#ffffff',
     alignItems: 'center'}}>
          4
        </Text>
     </View>
     <View style={{    position: 'absolute',
      top: 0,
      left: 20,
      width: 50,
      height: 50,
      marginLeft:210,
      borderWidth: 1,
       borderColor: '#000000',
       backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
      'center',
      marginTop:10,
      color: '#ffffff',
      alignItems: 'center'}}>
          5
        </Text>
     </View>
    <View style={{    position: 'absolute',
    top: 0,
    left: 20,
    width: 50,
    height: 50,
    marginLeft:260,
    backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
    'center',
   marginTop:10,
   color: '#ffffff',
   alignItems: 'center'}}>
          6
        </Text>
    </View>
    </View>
    </View>
    <View
     style={{
   borderBottomWidth: 2,
   borderBottomColor: 'black',
   width: 270,
   marginLeft:18,
   marginBottom:340,
   }}
   />

    <View style={{    position: 'absolute',
   top: 0,
   left: 20,
   width: 50,
   height: 50,
   marginLeft:40,
   marginTop:1
   }}>
    <View style={{    position: 'absolute',
  top: 0,
  left: 20,
  width: 50,
  height: 50,
  borderWidth: 1,
  borderColor: '#000000',
  marginTop:50,
  backgroundColor: '#006400'}}>

        <Text style={{justifyContent: 'center',flex: 1,textAlign: 
    'center',
    marginTop:10,
    color: '#ffffff',
     alignItems: 'center'}}>
          7
        </Text>
    </View>

    </View>

    <Button style={{marginTop:40}}  title="Roll again" onPress= 
     {this.GenerateRandomNumber} />
     </View>

我也尝试通过将此方法修改为

create(resource)
{  
  return this.http.post(this.url + 'saveRegistration.do',resource)
  .pipe(map((response:any)=>JSON.parse(response)),
   catchError(this.handleError));
}

并且也在下面尝试过

create(resource)
    {  
      return this.http.post(this.url + 'saveRegistration.do',resource)
      .pipe(map((response:any)=>JSON.stringify(response)),
       catchError(this.handleError));
    }

及以下

create(resource)
        {  
          return this.http.post(this.url + 'saveRegistration.do',resource)
          .pipe(catchError(this.handleError));
        }

有人可以帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:0)

如果您想在HttpPost中获得完整的响应,可以这样写

 return this.httpClient.post<User>(`${this.API_URL}`, resource, {
    observe: 'response'
 });

也无需解析为JSON,因为Angular默认返回JSON