2017-10-27 16 views
2

反応ネイティブ登録画面のエラーメッセージを返そうとしています。呼ばれる.PHPスクリプトは次のとおりです。JSON解析エラー:エラーメッセージを返すときにJSON文字列を解析できません

呼ば
<?php 

include 'DBConfig.php'; 

// Creating connection. 
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName); 
if ($con == false) { 
    $ShitMSG = "Can't connect to database" ; 
    $ShitJson = json_encode($ShitMSG); 
    echo $ShitJson ; 
} 

// Getting the received JSON into $json variable. 
$json = file_get_contents('php://input'); 
// decoding the received JSON and store into $obj variable. 
$obj = json_decode($json,true); 

// Populate User name from JSON $obj array and store into $name. 
$name = $obj['name']; 
// Populate User email from JSON $obj array and store into $email. 
$email = $obj['email']; 

//Checking Email is already exist or not using SQL query. 
$CheckSQL = "SELECT * FROM UserRegistrationTable WHERE email='$email'"; 
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL)); 
if(isset($check)) { 
    $EmailExistMSG = "L'E-mail est déja utilisé !"; 
    $EmailExistJson = json_encode($EmailExistMSG); 
    echo $EmailExistJson ; 
} 
else { 
    // Creating SQL query and insert the record into MySQL database table. 
    $Sql_Query = "insert into UserRegistrationTable (name,email) values ('$name','$email')"; 
    if(mysqli_query($con,$Sql_Query)) { 
    $MSG = 'Utilisateur enregistré !' ; 
    $json = json_encode($MSG); 
    echo $json ; 
    } 
    else { 
    echo 'Réessayez'; 
    } 
} 
mysqli_close($con); 

?> 

は、それが

body: JSON.stringify({ 
     name: UserName, 
     email: UserEmail 
     }) 

は私のユーザーの名前と電子メールの入力から成るように見える体を受け取ります。 DBconfig.phpは、dbに接続するためのPHPファイルです。

私のスクリプトは、私は、この関数から、ネイティブアプリを反応させることにより、起動される:

あなたに
class LoginScreen extends React.Component { 
    constructor (props) { 
    super(props) 
    this.state = { 
     UserName: '', 
     UserEmail: '' 
    } 
    } 

    UserRegistrationFunction() { 
    const { UserName } = this.state 
    const { UserEmail } = this.state 

    fetch('http://c2isante.fr/appsante/login.php', { 
     method: 'POST', 
     headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json' 
     }, 
     body: JSON.stringify({ 
     name: UserName, 
     email: UserEmail 
     }) 

    }).then((response) => response.json()) 
     .then((responseJson) => { 
     Alert.alert(responseJson) 
     }).catch((error) => { 
     console.error(error) 
     }) 
    } 

    render() { 
    return (
     <View style={styles.MainContainer}> 
     <Text style={{ fontSize: 20, color: '#000', textAlign: 'center', marginBottom: 15 }}>User Registration Form</Text> 
     <TextInput 
      placeholder='Entrez votre nom' 
      onChangeText={UserName => this.setState({...this.state, UserName})} 
      underlineColorAndroid='transparent' 
     /> 
     <TextInput 
      placeholder='Entrez votre E-mail' 
      onChangeText={UserEmail => this.setState({...this.state, UserEmail})} 
      underlineColorAndroid='transparent' 
     /> 
     <Button title="M'enregistrer" onPress={this.UserRegistrationFunction.bind(this)} color='#2196F3' /> 
     </View> 
    ) 
    } 
} 
+1

実際にPHPスクリプトの出力を確認する場所に反応コードを表示できますか? – RamaKrishna

+0

今すぐ追加! :) –

+0

**危険**:[SQLインジェクション攻撃](http://bobby-tables.com/)に対して脆弱** ** [防御]する必要があります(http://stackoverflow.com/あなた自身から/ 60174/best-way-to-prevent-sql-injection-in-php) – Quentin

答えて

0

PHPスクリプトあなたに直接json_encode文字列その見返りに、文字列のみ。

JavaScriptコードでPHPファイルから文字列を受け取り、文字列にJSON.parseを入れることはできません。それはあなたにエラーを与えます。

0

文字列をjson文字列にエンコードする際のポイントは何ですか?文字列を直接エコーするか、レスポンス配列を作成してエンコードします。

$x= array(
    "error_code": "404", 
    "message": "not found" 
); 
echo json_encode($x); 
0

あなたのPHPの3つの結果があります。

A JSONエンコードされた文字列:

$EmailExistMSG = "L'E-mail est déja utilisé !"; 
$EmailExistJson = json_encode($EmailExistMSG); 
echo $EmailExistJson ; 

A JSONエンコードされた文字列:

$MSG = 'Utilisateur enregistré !' ; 
$json = json_encode($MSG); 
echo $json ; 

プレーンテキスト文字列:

echo 'Réessayez'; 

あなたは一貫している必要があります:常にエンコードJSONまたはとして出力 JSONとして出力をエンコードすることはありません。

いずれか:

  1. 変更又は
  2. 変更最初の2つの出力平文変化JavaScriptにそれがないように他のようなJSONとして結果を符号化する第三のケース

別にJSONとしてそれを解析してみてください:あなたはを指定しませんなので、プレーンテキストやJSONを出力してもHTMLを出力すると主張しています(PHPのデフォルト)。それを修正する必要があります。

+0

私のアプリケーションのコードで期待通りにJSON文だけを返すようになりました –

関連する問題