gpt4 book ai didi

javascript - $.ajax 无法正常运行 - 返回 index.html

转载 作者:行者123 更新时间:2023-11-30 00:19:49 25 4
gpt4 key购买 nike

我正在尝试编写一个函数,允许我停止在我想使用它的每个表单和元素上编写冗余的 ajax 请求。这是一个非常简单的概念,但我似乎无法弄清楚它有什么问题。我只收到一个错误,但它与错误的 token 有关,因为 json 无效,但如果我删除 json 函数,它什么也不做。如果我用返回的数据填充一个容器,我只会得到一个索引页的副本。如果有所不同,这是从索引页面运行的。

我很好奇是否有人看到我在这里遗漏的任何错误...

谢谢!

这是代码

$(document).ready(function(){
/* ajax request standard functions
Optional attributes:
loadtype[html]: prepend, append, html(*complete page load*)
ajaxcon[error]: container to be affected by ajax
method[GET]: post/get
loader[progress1]: alternate load image other than the standard

*/
$(document).on('click', '.ajaxMe', function(e){
e.preventDefault();

var el = $(this); //a, li, form
var tag = el.prop('tagName'); //a, li, form
if(tag == 'FORM'){aType = 1;}else{aType = 2;} //sets default to a/li
var method = el.attr('method');
if(!method) method = 'GET'; //default method
var ajaxcon = el.attr('ajaxcon');
//if there's no ajax container to receive the data, return an error
if(!ajaxcon && aType != 1){
//later on, this should call a function that pops up the error box instead of an alert
alert("There seems to be a code error. Please contact support or try again later");
return false;
}
var loadtype = el.attr('loadtype');
if(!loadtype) loadtype = 'html'; //default loadtype set to html
var altloader = el.attr('altloader');
if(!altloader) altloader = 'http://localhost/mgo/img/gifs/loader.gif'; //default wait image

//set the variables that are determined by the parent element type
if(aType == 1){
var href = el.attr('action');
var sdata = el.serialize(); //We can serialize the data of all forms without checking because checking is going to be done on the php side from now on
}else if(aType == 2){
var href = el.attr('href');
var sdata = el.attr('rel');
}

/*JSON return layout:
return{
status: 0/1 -- included in case there is additional checking on the jquery side before/instead php redirect
message: message to display if bad
badInputs: inputs to highlight
}
*/

alert(sdata);
$.ajax({
type: method,
URL: href,
data: sdata,
success: function(ret){ //return is always going to be JSON
if(aType == 1){
//if data gets returned, it's an error. if no error, the php takes over and forces the next page
var r = $.parseJSON(ret);
el.find('.TopMsg')[loadtype](r.message);
}else if(aType == 2){
ajaxcon[loadtype](ret);
}
}
});
});
});

编辑为了好的措施,我添加了 html 和 php

HTML

<form class = 'Frm-cb ajaxMe' id = 'frmsignup' action = 'http://localhost/mgo/modules/signup/php/signup1.php'>
<h1 style = 'background-color: green;'>Sign up now for all the benefits of MGo!</h1>

<div class = 'TopMsg'></div>

<label>email</label>
<input type = 'text' name = 'email' id = 'email'>

<label>confirm email</label>
<input type = 'text' name = 'email2' id = 'email2'>

<label>password</label>
<input type = 'password' name = 'password' id = 'password'>

<label>confirm password</label>
<input type = 'password' name = 'password2' id = 'password2'>

<label>zip code</label>
<input type = 'text' name = 'zip' id = 'zip' maxlength = '5'>

<button type = 'submit'>finish</button>
</form>

PHP

<?

/*
This script is going to do the data validation for the jQuery so users can't hard code the scripts to change validation rules.
The output is JSON.
JSON output map:
[return]
[status]
[badInputs]
[inputname]
[msg]
[addClass]
[changeClass]
*/

include_once "C:/xampp/htdocs/mgo/scripts/php/connect/gen_user_db_connect.php";
include_once "C:/xampp/htdocs/mgo/scripts/php/validate/dataValidation.php";

$bi = array();
$msg = "";
$stat = 1;

$e1 = $_GET['email'];
$e2 = $_GET['email2'];
$p1 = $_GET['password'];
$p2 = $_GET['password2'];
$zip = $_GET['zip'];

$inputs = array("0", "username", "text");

$eChk = validate($e1)['email'];
$pChk = validate($p1)['len'];
$zChk = validate($zip);

if($eChk == 0){
$msg .= "Please enter a valid email address\n";
array_push($bi, "#email");
$stat = 0;
}

if($e1 != $e2){
$msg .= "Emails don't match\n";
array_push($bi, "#email2");
$stat = 0;
}

if($pChk < 6){
$msg .= "Password must be a minimum of 6 characters\n";
array_push($bi, "#password");
$stat = 0;
}

if($p1 != $p2){
$msg .= "Passwords don't match\n";
array_push($bi, "#password2");
$stat = 0;
}

if($zChk['num'] == 0){
$msg .= "Must enter a valid zip code\n";
$stat = 0;
}

$return = json_encode(array("msg" => "<pre>$msg</pre>",
"status" => $stat,
"badInputs" => $inputs));

echo $return;
?>

最佳答案

URL 参数的名称不应大写 -- "url"而不是 "URL"。

关于javascript - $.ajax 无法正常运行 - 返回 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com