AJAX响应成功

时间:2018-02-23 11:49:33

标签: php ajax

我有生成安全密码的功能。点击一个按钮,我进行了一个生成密码的ajax调用。当我尝试返回值,字符串或其他东西时,ajax不会通过访问,但日期会在数据库中更新...我无法找到无法正常工作的地方。

<input type="button" value="generate pass" class="btn btn-info mr50 auth-key-button" onclick="serialkey(<?=$_GET['id']?>)">

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

File generator.php where it generates the password:

$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; }

      Console.WriteLine("A");
      strings.ForEach( s => { AsyncMethod(s).Wait(); });

      Console.WriteLine("E");

1 个答案:

答案 0 :(得分:1)

有效...这就是我找到解决方案的方式,如果其他人需要它

 $data = "";
 $data['key'] = $key;
 echo json_encode($data);

我从ajax中删除dataType:'text / html',我使用var data = JSON.parse(data); 那是......