1 回答
TA贡献1841条经验 获得超3个赞
您从服务器获得的响应是纯文本格式,返回的数据是表单中的一系列行
ppp : nnn
其中ppp是 40 个字符长的 sha1 散列密码,nnn是发生次数。
您可以轻松地将响应转换为 PHP 中的关联数组,将数组转换为 JSON 并将 JSON 编码数据发送回前端 JavaScript:
$response = explode( "\n", $response );
$out = [];
foreach( $response as $r )
{
$r = explode( ":", $r );
$out[] = [ 'sha1' => $r[0], 'count' => $r[1] ];
}
$out = json_encode( $out );
echo $out;
JavaScript AJAXsuccess()回调将接收一个可以轻松检查的解码对象:
success: function(response) {
var found,
n,
i;
found = false;
n = response.length;
for( i = 0; i < n; i++ )
{
if( pw_sha1_to_check === response[i].sha1 )
{
found = true;
break;
}
}
if( found )
{
// `pw_sha1_to_check` was found in the list received as response
}
确保在使用 jQuery 进行 AJAX 调用时指定需要 JSON 响应:
$.ajax( {
dataType: 'json',
// ...
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报