ajax 関数の jQuery コードは次のとおりです。
$(document).ready(function() {
$("#zip_code").keyup(function() {
var el = $(this);
var module_url = $('#module_url').val();
if (el.val().length === 5) {
$.ajax({
url : module_url,
cache: false,
dataType: "json",
type: "GET",
data: {
'request_type':'ajax',
'op':'get_city_state',
'zip_code' : el.val()
},
success: function(result, success) { alert(result.join('\n'));
$("#city").val(result.place_name);
$("#state_code").val(result.state_code);
}
});
}
});
});
PHP コード スニペットは次のとおりです。
case "get_city_state":
// to get the city and state on zip code.
$ret = $objUserLogin->GetCityState($request);
if(!$ret) {
$error_msg = $objUserLogin->GetAllErrors();
list($data) = prepare_response($request);
$smarty->assign('data', $data);
} else {
$data = $objUserLogin->GetResponse();
echo $data;
}
die;
break;
PHP コードでは、$data には次の方法でデータが含まれます。
<pre>Array
(
[id] => 23212
[zip_code] => 28445
[place_name] => Holly Ridge
[state_code] => NC
[created_at] => 1410875971
[updated_at] => 1410875971
)
</pre>
上記のデータ (つまり、変数で使用できる応答は ajax 応答になります) から、place_name と state_code の 2 つのフィールドのみにアクセスしたいと考えています。
alert(result)
コンソールで使用して結果変数の内容を印刷しようとしましたが、単語が表示されますArray
これを達成する方法は私の疑問ですか?
前もって感謝します。