php - JSON 中第 2 位的意外标记 N
问题描述
嗨,你们能找出错误在哪里吗?作为初学者离子开发人员,我总是很难处理我的 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);
?>
解决方案
您的问题是脚本输出的所有内容最终都出现在您的 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));
推荐阅读
- python - 在排序一个列表期间将不同列表中的相关元素保持在一起
- c# - 如何对通用列表对象进行排序
- javascript - 向和从 js 服务器发送数据的基本方法?
- json - 使用 Pyspark 处理 JSON 结构
- amazon-web-services - 为什么角色假设应该在 lambda 内部完成?
- html - 如何将数值绑定到 CSS 以创建时间线?
- docker - 如何为 docker compose 环境变量设置运行时变量
- javascript - 仅在 Javascript 中具有不同段落的动态 Div
- tsql - 表格模型中的测量列未在浏览器中显示正确的值
- javascript - 调用此函数时如何获取返回值?