AJAX 总是返回到主页
AJAX always returning into Home Page
>有没有机构可以帮助我解决问题。我有 3 个文件index.php
process.js
redirect_process.php
.好吧,index.php
是我的主页,里面有一个表格。并且每次单击button submit
时都会调用process.js
以验证表单的数据,因此,如果输入数据有错误,只需不要返回 true 并显示一些错误消息,否则返回 true,然后重定向到另一个页面redirect_process.php
。这是我的process.js
代码:
$(document).ready(function(){
$('#form_side').submit(function() {
var name_this = /^[a-zA-Z-' ]+$/;
var return_process = false;
if(name_this.test(document.getElementById("input_field_one").value) == false){
alert('Error!');
return_process = false;
}else{
$.ajax({
type:"POST",
url:"./r_p/redirect_process.php",//the link to call redirect process
data: $("#my_form").serialize(),
success: function(response){
$("#my_form").trigger('reset');
} });
//No more validation here, so i want to go into another page now by returning into TRUE
return_process = true; //This will be the cause now to go to another page.
}
return return_process;
});
});
所以让我们假设现在没有错误输入的数据,我现在想使用 redirect_process.php
重定向到另一个页面。这是我的redirect_process.php
代码:
...Some validation here
if (data is ok, and no error) {
//Redirect into correct page
header('Location: http://localhost/test/correct_page.php');
}else{
//Redirect into wrong page
header('Location: http://localhost/test/wrong_page.php');
}
问题是,它总是返回到index.php
它没有在上面的任何链接中重定向。请我帮帮我。谢谢!
可以肯定的是,一切都会发生在你的索引中.php因为你在 PHP 文件中调用 header,你把它与 ajax 一起使用,如果你直接去redirect_process.php,标题会起作用(并且始终在 header();之后使用 exit();
在 PHP 中执行此操作
...Some validation here
if (data is ok, and no error) {
echo('OK');
}else{
echo('Not OK');
}
然后在 js 中的提交事件中
$(document).ready(function(){
$('#form_side').submit(function(e) {
e.preventDefault; // use e.preventDefault;
var name_this = /^[a-zA-Z-' ]+$/;
var return_process = false;
if(name_this.test(document.getElementById("input_field_one").value) == false){
alert('Error!');
return_process = false;
}else{
$.ajax({
type:"POST",
url:"./r_p/redirect_process.php",//the link to call redirect process
data: $("#my_form").serialize(),
success: function(response){
$("#my_form").trigger('reset');
if(response == 'OK'){
window.location.href= "http://localhost/test/correct_page.php";
}else{
window.location.href= "http://localhost/test/wrong_page.php";
}
} });
//No more validation here, so i want to go into another page now by returning into TRUE
return_process = true; //This will be the cause now to go to another page.
}
return return_process;
});
});
这应该可以做到:
$(document).ready(function(){
$('#form_side').submit(function(event) {
var name_this = /^[a-zA-Z-' ]+$/;
var return_process = false;
if(name_this.test(document.getElementById("input_field_one").value) == false){
alert('Error!');
return_process = false;
}else{
$.ajax({
type:"POST",
url:"./r_p/redirect_process.php",//the link to call redirect process
data: $("#my_form").serialize(),
success: function(response){
$("#my_form").trigger('reset');
} });
}
event.preventDefault(); //this line prevents the form from executing it's action, which is none and that triggers a reload.
return false;
});
});
您正在通过 jQuery.submit
调用 onsubmit 事件。解析此事件时,将触发表单操作。由于没有定义任何内容,因此您使用 AJAX,因此会触发重新加载。
event.preventDefault()
这可以防止事件触发操作。
...Some validation here
if (data is ok, and no error) {
//Redirect into correct page
header('Location: http://localhost/test/correct_page.php');
}else{
//Redirect into wrong page
header('Location: http://localhost/test/wrong_page.php');
}
标题行有效,但不适用于您的情况。它告诉浏览器加载页面响应。此响应加载到 AJAX 响应中,而不是重定向当前页面。使用@Mohamed-Yousef的答案作为指导。
- 节点导出返回一个空对象
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 控制台返回var不是't定义,但它是
- 从函数返回角度承诺
- iframe返回并避免主页返回
- 单击旋转木马外部返回主页
- Ajax在响应后返回主页
- 带有参数的 Angular JS 面包屑返回主页
- AJAX 总是返回到主页
- 如何将控制从php脚本返回到我的主页
- Jquery验证插件,如何检查PHP文件是否返回错误或可以将用户重定向到主页/dashbord
- 如何使用dxAction dxButton返回主页
- Ui-router返回主页按钮
- NavigatorView's的页面堆栈是空的,返回到主页Onsen UI
- 如果在汉堡菜单中导航,点击“返回”按钮将返回主页
- 我试图使用ng-hide来防止返回按钮在主页上显示
- Jquery:重载主页使用其他页面返回值
- 带有Perl脚本的HTML不会返回主页
- 在 ie9 及更低版本中使用历史记录.js页面刷新将返回到主页