Php Vuejs ajax调用不起作用
大家好,我正在尝试制作一个vueapp,用于对php页面进行ajax调用。在php页面上,我运行了几个mysql语句,其中最重要的是insert语句。如果页面ajax调用成功并且insert语句成功运行,我想重定向页面。如果ajax调用或insert语句失败,或者用户存在,我想将它们重定向到同一个页面,只需根据结果不同地呈现页面。我曾经尝试过使用sessions,但是当我尝试使用ajax错误函数时,它每次都会触发,即使它成功了。我知道这一点,因为我得到了一个警报错误。但是当它转到result.php页面时,我获得了成功。我不明白。任何帮助都会很好Php Vuejs ajax调用不起作用,php,ajax,vue.js,session-variables,Php,Ajax,Vue.js,Session Variables,大家好,我正在尝试制作一个vueapp,用于对php页面进行ajax调用。在php页面上,我运行了几个mysql语句,其中最重要的是insert语句。如果页面ajax调用成功并且insert语句成功运行,我想重定向页面。如果ajax调用或insert语句失败,或者用户存在,我想将它们重定向到同一个页面,只需根据结果不同地呈现页面。我曾经尝试过使用sessions,但是当我尝试使用ajax错误函数时,它每次都会触发,即使它成功了。我知道这一点,因为我得到了一个警报错误。但是当它转到result.p
<?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');?>
?>
用户名*
密码*
确认密码*
名字*
姓*
电子邮件*
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{$axjaxworked=0;}
if(isset($_SESSION['inserted'])){$inserted=1;} else {$inserted=0;}
if($inserted==1 and $ajaxworked==1){echo 'success';}
if($inserted==0 and $ajaxworked==0){echo 'failed';}
session_destroy();
?>
此用户名不存在
也可以直接执行此操作,而不是通过变量传递:
window.location.href="successfullycreated.php?userName="+ <?php echo "'{$_GET['userName']}'";?>;
window.location.href=“successfullycreated.php?userName=“+此.userName
不存在
也可以直接执行此操作,而不是通过变量传递:
window.location.href="successfullycreated.php?userName="+ <?php echo "'{$_GET['userName']}'";?>;
window.location.href=“successfullycreated.php?userName=“+只要新页面存在参数,您就可以从JavaScript中的URL获取它
var userName = new URL(window.location.href).searchParams.get("userName");
只要新页面有参数,您就可以从JavaScript中的URL获取它
var userName = new URL(window.location.href).searchParams.get("userName");
没有相同的问题没有相同的问题