我在pge上有一个按钮,它从php页面获取json数据,
数据似乎到了好但我已经经历了数百个例子,我似乎无法引用返回的数据,这是我的脚本:
以前评论中的编辑脚本
$('#geo_batch').click(function (){
var ajax_load = "<label><img src='/images/icons/loadinfo.gif' alt='saving location...' /> Loading data...</label>";
$("#batch_detail").html(ajax_load);
$('#batch_buttons').hide();
var form = $("form"); //Grab the form element from the DOM
//alert(form.serialize());
var mydata = form.serialize();
$.ajax({
type: "POST",
url: 'geo_getupdate_list.php',
data: mydata,
datatype: 'json',
success: function(dat) {
alert('dat:'+ typeof dat ) //RETURNS STRING
//alert(dat.location[0].id_mdt); //RETURNS UNDEFINED
// Cache the batch_detail element
var $detail = $("#batch_detail").html('<label>Locations have been retrieved:<br>' + dat + '<label>');
$('#batch_buttons').show();
// Instead of several .append() calls on the same element, create a
// single string, and do one.
var appendString = '';
for(var key in dat) { alert(key); return false; };
/*for(i=0; i < count; i++){
appendString += 'display address: ' + data.location[i].displayaddr_mdt + 'flag: ' + data.location[i].flag_mdt;
}*/
$detail.append(appendString);
},
error: function(dat) { //Triggered if an error communicating with server
//alert('fail');
$("#batch_detail").html('<label>There was an error: '+dat+'<label>');
$('#batch_buttons').show();
}
});
return false; //Ignore the default behavior of the button click
});
返回的json是:
{"location":[{"id_mdt":"5","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"1","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"via todde 29 Ales Sardegna 09091","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"39.7670964","lng_mdt":"8.813689","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"4","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"3","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"6","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":null,"lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"7","idetp_mdt":"1","name_mdt":"Test","geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":null,"lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null}]}
如何访问数组中的数据?
我在这里和其他地方尝试了很多例子,我无法工作。 我认为它可能与它所拥有的返回的json对象有关[我认为它是错误的符号吗?
php我必须生成json如下://have defined a recordset called $rs_locations
$rows = array();
while($r = mysql_fetch_assoc($rs_locations)) {
$rows[] = $r;
}
$jsondata = json_encode($rows);
// trying to strip out the [ ] brackets but doesn't work
str_replace ("[", "", $jsondata);
str_replace ("]", "", $jsondata);
echo($jsondata);
任何人的想法,我很困惑,谢谢
答案 0 :(得分:3)
编辑:您的dataType属性拼写错误。
应该是dataType
,而不是datatype
。
另外,请尝试将data
参数更改为其他名称,例如dat
。在$.ajax()
来电设置data
属性之前,我已经看到了相关问题。
success: function( dat ) {
// Then change all references from data to dat
尝试使用success:
回拨。
您多次获取相同的#batch_detail
元素并不断在该元素上调用.append()
。
这样你就可以缓存对元素的引用,创建一个要追加的String,然后在循环结束后执行一次追加。
您遇到的具体问题是您需要直接引用data.location
存储的数组。
success: function(dat) {
// Cache the batch_detail element
var $detail = $("#batch_detail").html('<label>Locations have been retrieved:<br>' + dat + '<label>');
$('#batch_buttons').show();
var count = dat.location.length - 1;
// Instead of several .append() calls on the same element, create a
// single string, and do one.
var appendString = '';
for(i=0; i < count; i++){
appendString += 'display address: ' + dat.location[i].displayaddr_mdt + 'flag: ' + dat.location[i].flag_mdt;
}
$detail.append( appendString );
},
答案 1 :(得分:0)
如果我的脚本正确,您必须先从location
获取data
数组:
var locations = data.location;
for(var location in locations) {
console.debug(locations[location]);
}