Php Vuejs Ajax调用自动触发错误函数
大家好,我正在尝试制作一个vueapp,用于对php页面进行ajax调用。在php页面上,我运行了几个mysql语句,其中最重要的是insert语句。如果页面ajax调用成功并且insert语句成功运行,我想重定向页面。如果ajax调用或insert语句失败,或者用户存在,我想将它们重定向到同一个页面,只需根据结果不同地呈现页面。我曾经尝试过使用sessions,但是当我尝试使用ajax错误函数时,它每次都会触发,即使它成功了。我知道这一点,因为我得到了一个警报错误。但是当它转到result.php页面时,我获得了成功。我不明白。任何帮助都会很好以下是vueapp(signup.php)的代码Php Vuejs Ajax调用自动触发错误函数,php,mysql,ajax,vue.js,Php,Mysql,Ajax,Vue.js,大家好,我正在尝试制作一个vueapp,用于对php页面进行ajax调用。在php页面上,我运行了几个mysql语句,其中最重要的是insert语句。如果页面ajax调用成功并且insert语句成功运行,我想重定向页面。如果ajax调用或insert语句失败,或者用户存在,我想将它们重定向到同一个页面,只需根据结果不同地呈现页面。我曾经尝试过使用sessions,但是当我尝试使用ajax错误函数时,它每次都会触发,即使它成功了。我知道这一点,因为我得到了一个警报错误。但是当它转到result.p
用户名*
密码*
确认密码*
名字*
姓*
电子邮件*
var app=新的Vue({
el:“#报名表”,
数据:{
用户名:“”,
密码:“”,
确认密码:“”,
名字:'',
姓氏:“”,
电子邮件:“”
},
计算:{
密码匹配:函数(){
if(this.password==this.confirmPassword){
返回true;
}否则{
返回false;
}
},
passwordRequirementsMet:function(){
如果(this.password.length>=8){
返回true;
}否则{
返回false;
}
},
validEmail:function(){
变量reg=/^([A-Za-z0-9\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
如果(!注册测试(此电子邮件)){
返回false;
}
返回true;
}
},
已创建:函数(){
},
观察:{
},
方法:{
makeAccount:function(){
if(this.userName.length>=8&&this.firstName!=''&&this.lastName!=''&&this.validEmail&&this.passwordRequirementsMet&&this.passwordsMatch){
var jsonString=JSON.stringify({
用户名:this.userName,
名字:这个,名字,
lastName:this.lastName,
密码:this.password,
电子邮件:this.email
});
$.ajax({
url:'makeAccount.php',
数据类型:“json”,
键入:“post”,
contentType:'应用程序/json',
数据类型:“json”,
资料来源:jsonString,
错误:函数(){
警报(“错误”);
window.location.href='result.php';
},
成功:功能(数据){
控制台日志(数据);
警惕(“成功”);
window.location.href='result.php';
}.绑定(此)
});
}
}
}
});
?>
下面是php页面的代码,我正在向该页面发出ajax请求。(makeAccount.php)
下面是result.php的代码
<?php session_start();
if(isset($_SESSION['hitpage'])){$ajaxworked=1;} else{$ajaxworked=0;}
if(isset($_SESSION['inserted'])){$inserted=1;} else {$inserted=0;}
if(isset($_SESSION['userexists'])){$userexists=1;} else{$userexists=0;}
if($inserted==1 and $ajaxworked==1){echo 'Congrats you seccessfully created your account click <a href="index.php">here</a> to go home';}
if($ajaxworked==0){echo 'Possible time out if error prosists please contact creative group click <a href="signuptest.php">here</a> to try again';}
if($inserted==0 and $ajaxworked==1 and $userexists==0){echo 'There was an error creating your account if error prosists please contact the creative group click <a href="signuptest.php">here</a> to try again';}
if($userexists==1){echo 'Sorry that user already exists click <a href="signuptest.php">here</a> to try again';}
session_destroy();
?>
我流了很多泪,头撞在屏幕上。向太阳底下的每一位上帝祈祷,并进行大量的咒骂,我终于找到了问题所在。我的JSON字符串无效
<?php session_start();
$_SESSION['hitpage']=1;
require_once('database.php');
require_once('functions.php');
$requestBody = file_get_contents('php://input');
$requestJSON = json_decode($requestBody);
require_once 'lib/Braintree.php';
$gateway = new Braintree_Gateway([
'environment' => 'sandbox',
'merchantId' => '*****',
'publicKey' => '*****',
'privateKey' => '*****'
]);
$braintreeResponse = $gateway->customer()->create([
'firstName' => $requestJSON->firstName,
'lastName' => $requestJSON->lastName,
'email' => $requestJSON->email
]);
if ($braintreeResponse->success) {
echo(json_encode($braintreeResponse));
} else {
echo(json_encode($braintreeResponse));
}
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$googleResponse = get_data('https://script.google.com/macros/s/AKfycbzz8Oh3Jqt5tP4wGcNyM8jVhwaMEr6S5AJ-MWqFlhPN1rSzBdSr/exec?name='.urlencode(stripslashes($requestJSON->userName)));
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$googleResponseParsed = get_string_between($googleResponse, '<title>', '</title>');
echo($googleResponseParsed);
$username = $requestJSON->userName;
$username = mysqli_real_escape_string($mysqli, $username);
$firstname = $requestJSON->firstName;
$firstname = mysqli_real_escape_string($mysqli, $firstname);
$lastname = $requestJSON->lastName;
$lastname = mysqli_real_escape_string($mysqli, $lastname);
$email = $requestJSON->email;
$email = mysqli_real_escape_string($mysqli, $email);
$cleanGoogleResponseParsed = $googleResponseParsed;
$cleanGoogleResponseParsed = mysqli_real_escape_string($mysqli, $cleanGoogleResponseParsed);
$customerid = $braintreeResponse->customer->id;
$customerid = mysqli_real_escape_string($mysqli, $customerid);
$password = $requestJSON->password;
$encryptedpassword=password_encrypt($password);
$selectcount="SELECT COUNT(*) as exist FROM user WHERE userName='$username'";
$countresult=mysqli_query($mysqli, $selectcount);
while($row=mysqli_fetch_assoc($countresult)){
$exists=$row['exist'];
}
if($exists==0){
$makeUserSQL = "INSERT INTO user (userName, firstName, lastName, email, driveFolderId, braintreeId, password)
VALUES ('".$username."','".$firstname."','".$lastname."','".$email."','".$cleanGoogleResponseParsed."','".$customerid."','".$encryptedpassword."')";
if ($mysqli->query($makeUserSQL) === TRUE) {
echo "New record created successfully";
$_SESSION['inserted']=1;
} else {
echo "Error: " . $makeUserSQL . "<br>" . $mysqli->error;
}
} else {$_SESSION['userexists']=1;}
$mysqli->close();
?>
<?php session_start();
if(isset($_SESSION['hitpage'])){$ajaxworked=1;} else{$ajaxworked=0;}
if(isset($_SESSION['inserted'])){$inserted=1;} else {$inserted=0;}
if(isset($_SESSION['userexists'])){$userexists=1;} else{$userexists=0;}
if($inserted==1 and $ajaxworked==1){echo 'Congrats you seccessfully created your account click <a href="index.php">here</a> to go home';}
if($ajaxworked==0){echo 'Possible time out if error prosists please contact creative group click <a href="signuptest.php">here</a> to try again';}
if($inserted==0 and $ajaxworked==1 and $userexists==0){echo 'There was an error creating your account if error prosists please contact the creative group click <a href="signuptest.php">here</a> to try again';}
if($userexists==1){echo 'Sorry that user already exists click <a href="signuptest.php">here</a> to try again';}
session_destroy();
?>