[英]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.