json retrival failed with jquery .each
{"paging":
{"pageNum":2,
"action":"Next",
"type":"",
"availableCacheName":"getAllFunds",
"selectedCacheName":"",
"showFrom":101,
"showTo":200,
"totalRec":289,
"pageSize":100},
"Data": [{"sourceCodeId":0,
"radio_fund":"individual",
"availableFunds":[],
"fundId":288,
"searchName":[],
"fundName":"Asian Equity Fund A Class Income",
"srcFundGrpId":"PGI",
"firstElement":0,
"lastElement":0,
"totalElements":0,
"pageList":[],
"standardExtract":true}]
}
I have json file with above format with two fileds,one paging and one is Data array.
I able to retrieve values of paging,but i am not able to retrieve the values of data array with .each function of jquery.
Any suggestions or inputs really appreciated.
$.ajax({ url: "list.pubfw",
data :{action:action},
dataType: "json",
type:"POST",
success: function(result){
var options = '';
$.each(result, function(){
options += '<option value="' +
result.jsonData.fundId + '">' +
result.jsonData.fundId + "-" +
result.jsonData.fundName + "-" +
result.jsonData.srcFundGrpId + '</option>';
});
$("#selectCol").empty();
$("#selectCol").html(options);
},
error: function(xmlHttpRequest, textStatus, errorThrown){
alert("ERROR"+errorT开发者_运维技巧hrown);
alert("STAT"+textStatus);
alert("xmlHttpRequest"+xmlHttpRequest);
}
});
There could be an error in the JSON validation, check Json Lint, since JSON is tricky.
Okay, here are two notes:
1.First of all you should use result.Data
and not result.jsonData
2.the Data
field holds an Array of objects you you'll have to loop over it or use indexes
3.If the Data
field will not hold more than one Item then no need for the $.each
method so you can easily use:
result.Data[0].fundId
..etc
But since it's an array of object then I would assume that it may contain more items so each is needed but not the way you are using it, instead use this
My guess is that you need to change the data part from:
data: {action: action}
To:
data: {"action": action}
This way, on the server side you can check the "action" property of the request coming in, instead of having the property name be as variable as its value.
On a side note, I would change your success function to:
success: function(result){
var selectCol = $('#selectCol');
selectCol.empty();
$.each(result, function(){
var option = $('<option/>');
option.val(result.jsonData.fundId);
option.text(
result.jsonData.fundId + "-" +
result.jsonData.fundName + "-" +
result.jsonData.srcFundGrpId);
selectCol.append(option);
});
}
This keeps from building a (possibly) very big string, and it's also safer to use text()
than html()
, because html()
will just insert anything that came in as html, leaving you vulnerable to XSS attacks. text()
will html encode whatever comes in.
精彩评论