vuejsajax调用aways激发错误函数

ar7v8xwq  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(289)

大家好,我正在尝试制作一个vueapp,它可以对php页面进行ajax调用。在那个php页面上,我运行了几个mysql语句,其中最重要的是insert语句。如果页面ajax调用成功并且insert语句成功运行,我想重定向一个页面。如果ajax调用或insert语句失败,或者用户存在,我想将它们重定向到相同的页面,只是根据结果呈现不同的页面。我曾经尝试过使用sessions,但是当我尝试使用ajax错误函数时,它每次都会触发,即使它成功了。我知道这一点,因为我得到一个警报错误。但是当它转到result.php页面时,我获得了成功。我不明白。任何帮助都太好了!下面是vueapp的代码(signup.php)

<?php ob_start();
session_start();
include('head.php');
include('header.php');
?>
<div id="signUpFormContainer">
  <div id="signUpForm">
    <div class="inputDiv">
      <p>Username*</p>
      <input v-model="userName" placeholder="joseChang">
    </div>
    <div class="inputDiv">
      <p>Password*</p>
      <input type="password" v-model="password" placeholder="********">
    </div>
    <div class="inputDiv">
      <p>Confirm Password*</p>
      <input type="password" v-model="confirmPassword" placeholder="********">
    </div>
    <div class="inputDiv">
      <p>First Name*</p>
      <input v-model="firstName" placeholder="Jose">
    </div>
    <div class="inputDiv">
      <p>Last Name*</p>
      <input v-model="lastName" placeholder="Chang">
    </div>
    <div class="inputDiv">
      <p>Email*</p>
      <input v-model="email" placeholder="jchang@example.com">
    </div>
    <div class="inputButton">
      <input v-on:click.prevent="makeAccount" id="addButton" type="button" value="Sign Up"></input>
    </div>
  </div>
</div>
<div id="footerContainer"></div>

<script>
var app = new Vue({
  el: '#signUpForm',
  data: {
    userName: '',
    password: '',
    confirmPassword: '',
    firstName: '',
    lastName: '',
    email: ''
  },
  computed: {
    passwordsMatch: function() {
      if(this.password == this.confirmPassword) {
        return true;
      } else {
        return false;
      }
    },
    passwordRequirementsMet: function() {
      if(this.password.length >= 8) {
        return true;
      } else {
        return false;
      }
    },
    validEmail: function() {
      var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      if (!reg.test(this.email)) {
        return false;
      }
      return true;
    }
  },
  created: function() {
  },
  watch: {
  },
  methods: {
    makeAccount: function() {
      if(this.userName.length >= 8 && this.firstName != '' && this.lastName != '' && this.validEmail && this.passwordRequirementsMet && this.passwordsMatch) {
        var jsonString = JSON.stringify({
          userName: this.userName,
          firstName: this.firstName,
          lastName: this.lastName,
          password: this.password,
          email: this.email
        });
        $.ajax({
          url: 'makeAccount.php',
          dataType: 'json',
          type: 'post',
          contentType: 'application/json',
          dataType: 'json',
          data: jsonString,
          error: function(){
          alert('Error');
           window.location.href='result.php';
          },
          success: function(data){
            console.log(data);
            alert('success');
           window.location.href='result.php';
          }.bind(this)
        });
     }
    }
    }
  });

</script>
<?php include('foot.php');?>

?>

下面是php页面的代码,我正在向它发出ajax请求(makeaccount.php)

<?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();

?>

下面是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();
?>
oyjwcjzk

oyjwcjzk1#

我流了很多眼泪,头撞在屏幕上。向太阳底下的每一位上帝祈祷,以及大量的咒骂,我终于找到了问题所在。我的json字符串无效

相关问题