Php AJAX响应成功
我有一个生成安全密码的函数。只要点击一个按钮,我就会进行一个ajax调用,生成一个密码。当我尝试返回值、字符串或其他内容时,ajax不会传递访问权限,但数据库中的日期会更新。。。我找不到哪里出了问题 以下是按钮: 生成密码的文件生成器.php:Php AJAX响应成功,php,ajax,Php,Ajax,我有一个生成安全密码的函数。只要点击一个按钮,我就会进行一个ajax调用,生成一个密码。当我尝试返回值、字符串或其他内容时,ajax不会传递访问权限,但数据库中的日期会更新。。。我找不到哪里出了问题 以下是按钮: 生成密码的文件生成器.php: 工作。。。这就是我如何在别人需要时找到解决方案的原因 $data = ""; $data['key'] = $key; echo json_encode($data); 我从ajax中删除数据类型:“text/html”,并使用var data=
工作。。。这就是我如何在别人需要时找到解决方案的原因
$data = "";
$data['key'] = $key;
echo json_encode($data);
我从ajax中删除数据类型:“text/html”,并使用var data=JSON.parsedata;
那就是 因此,您使结果元素可见,但从不使用数据填充它。这可能是你悲伤的原因吗?我也不知道为什么你使用本机方法来附加事件侦听器,尽管手头有jQuery。不。。。我试图填充输入值,但没有。。。我尝试返回json_encode$data,并在ajax中返回var data=json.parsedata。。。但是没有结果…警报2?而你没有在任何地方显示出反应。。。更改为alertdata,例如,您只显示结果元素,但没有值,因此添加行$'result'.htmldata以包括AJAX调用的响应。
<script>
document.querySelector(".auth-key-button").addEventListener("click", function(){
document.querySelector(".auth-key-inputbox").style.display = "block";
});
function serialkey(id) {
$.ajax({
type: "POST",
url: 'generator.php',
dataType: 'text/html',
data:{
id: id,
action:'call_this'
},
success:function(data) {
alert(2);
$( "#result" ).show();
$( "#divKey" ).hide();
}
});
}
$user = dbUse( "SELECT * FROM users WHERE id = ".$_POST['id']." ORDER BY id ASC;" );
if($_POST['action'] == 'call_this') {
function incrementalLetter($len = 1){
$charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$base = strlen($charset);
$result = '';
$now = explode(' ', microtime())[1];
while ($now >= $base){
$i = $now % $base;
$result = $charset[$i] . $result;
$now /= $base;
}
return substr($result, -1);
}
function random($length, $chars = '')
{
if (!$chars) {
$chars = implode(range('a','f'));
$chars .= implode(range('0','9'));
$time = -microtime(true);
$hash = 0;
for ($i=0; $i < rand(1000,4000); ++$i) {
$hash ^= sha1(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,10)));
}
$chars .= $time + microtime(true);
}
$shuffled = str_shuffle($chars);
$shuffled = str_replace('.', incrementalLetter(), $shuffled);
return substr($shuffled, 0, $length);
}
$key = random(6).'-'.random(6).'-'.random(6).'-'.random(6).'-'.random(6).'-'.random(6);
$unique = dbUse( "SELECT auth_key, COUNT(*) c FROM users GROUP BY auth_key HAVING c>1;" );
if(count($unique)>0){
random(6).'-'.random(6).'-'.random(6).'-'.random(6).'-'.random(6).'-'.random(6);
}else{
dbUse( "UPDATE users SET auth_key = '".$key."' WHERE id= ".$_POST['id'].";" );
}
return $key;
}
$data = "";
$data['key'] = $key;
echo json_encode($data);