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 關注
- 129 瀏覽
添加回答
舉報