繁体   English   中英

JSON 中的意外令牌 N 在 position 2

[英]Unexpected token N in JSON at position 2

嗨,你们能找出错误在哪里吗? 作为初学者离子开发人员,我总是很难在我的 PHP 文件中使用敏感的 JSON 希望你们花时间回答这个问题。 作为初学者离子开发人员,我总是很难在我的 PHP 文件中使用敏感的 JSON 希望你们花时间回答这个问题。

注册.ts

import { Component, ViewChild } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { HomePage } from '../home/home';
import { Http, Headers, RequestOptions }  from "@angular/http";
import { LoadingController } from 'ionic-angular';
import 'rxjs/add/operator/map';

@Component({
  selector: 'page-register',
  templateUrl: 'register.html'
})
export class RegisterPage {


@ViewChild("email") email;
@ViewChild("username") username;
@ViewChild("mobile") mobile;
@ViewChild("userpass") userpass;


  constructor(public navCtrl: NavController, public alertCtrl: AlertController,  private http: Http,  public loading: LoadingController) {

  }

  Register(){
 //// check to confirm the username, email, telephone and userpass fields are filled

  if(this.username.value=="" ){

 let alert = this.alertCtrl.create({

 title:"ATTENTION",
 subTitle:"Username field is empty",
 buttons: ['OK']
 });

 alert.present();
  } else
 if(this.email.value==""){

 let alert = this.alertCtrl.create({

 title:"ATTENTION",
 subTitle:"Email field is empty",
 buttons: ['OK']
 });

 alert.present();

}
 else 
  if(this.mobile.value=="" ){

 let alert = this.alertCtrl.create({

 title:"ATTENTION",
 subTitle:"Mobile number field is empty",
 buttons: ['OK']
 });

 alert.present();
  } else
 if(this.userpass.value==""){

 let alert = this.alertCtrl.create({

 title:"ATTENTION",
 subTitle:"userpass field is empty",
 buttons: ['OK']
 });

 alert.present();

}
 else 
 {


var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let options = new RequestOptions({ headers: headers });

  let data = {
        username: this.username.value,
        userpass: this.userpass.value,
        mobile: this.mobile.value,
        email: this.email.value      
      };


 let loader = this.loading.create({
    content: 'Processing please wait...',
  });

 loader.present().then(() => {
this.http.post('http://localhost/mobile/register.php',data, options)
.map(res => res.json())
.subscribe(res => {

 loader.dismiss()
if(res=="Registration successfull"){
  let alert = this.alertCtrl.create({
    title:"CONGRATS",
    subTitle:(res),
    buttons: ['OK']
    });

    alert.present();
 this.navCtrl.push(HomePage);

}else
{
 let alert = this.alertCtrl.create({
 title:"ERROR",
 subTitle:(res),
 buttons: ['OK']
 });

 alert.present();
  } 
});
});
 }

}
}

寄存器.php

<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }

  require "dbconnect.php";

    $data = file_get_contents("php://input");
    if (isset($data)) {
        $request = json_decode($data);
        $username = $request->username;
        $userpass = $request->userpass;
        $mobile = $request->mobile;
        $email = $request->email;
    }

$username = stripslashes($username);
$userpass = stripslashes($userpass);
$userpass = sha1($userpass."@la]#}^(dy*%-Ga=/,Ga!.");

$sql = "INSERT INTO users (username,userpass,mobile,email)
        VALUES ('$username','$userpass','$mobile','$email')";

if ($con->query($sql) === TRUE) {
    echo "New account created successfully";
} 

else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

    echo json_encode($data);

?>

在此处输入图像描述 在此处输入图像描述

您的问题是脚本中 output 的所有内容最终都会出现在您的 ajax 响应中。 由于您尝试将其解码为JSON ,因此您需要确保它只包含它。 因此,与其回显状态消息,不如将其包含在 JSON 中,并带有success状态或类似状态。 像这样的东西:

if ($con->query($sql) === TRUE) {
    $status = "success";
    $message = "New account created successfully";
} 
else {
    $status = "error";
    $message =  "Error: " . $sql . "<br>" . $con->error;
}

echo json_encode(array('status' => $status, 'message' => $message, 'data' => $data));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM