I have a json array printed out from php file. And then tried to get elements by parsing it. But I am confused where is wrong.
<?php
$json = array("a1", "a2", "a3", "a4", "a5".......);
echo json_encode($json);
?>
<script>
$.ajax({
/*other codes*/
success : function(data){
var json = parseJSON(data);
for(var i = 0 ; i < json.length; i++){
alert(json[i]);
}
}
Use
$.ajax({
dataType: "json",
...
The response (data) is in JSON.
If data
is a JSON String
then you can use JSON.parse()
method to convert it into JSON Object and then we can parse it using Array map() method or for...in loop.
var json = JSON.parse(data);
var result = json.map(function(item) {
return item;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.