I'm trying to retrieve data in a javascript file from a php file using json.
我正在尝试使用json从php文件中检索javascript文件中的数据。
$items = array();
while($r = mysql_fetch_array($result)) {
$rows = array(
"id_locale" => $r['id_locale'],
"latitudine" => $r['lat'],
"longitudine" => $r['lng']
);
array_push($items, array("item" => $rows));
}
ECHO json_encode($items);
and in the javascript file I try to recover the data using an ajax call:
在javascript文件中,我尝试使用ajax调用恢复数据:
$.ajax({
type:"POST",
url:"Locali.php",
success:function(data){
alert("1");
//var obj = jQuery.parseJSON(idata);
var json = JSON.parse(data);
alert("2");
for (var i=0; i<json.length; i++) {
point = new google.maps.LatLng(json[i].item.latitudine,json[i].item.longitudine);
alert(point);
}
}
})
The first alert is printed, the latter does not, it gives me error: Unexpected token <.... but I do not understand what it is.
打印出第一个警报,后者没有,它给了我错误:意外的令牌<....但我不明白它是什么。
Anyone have any idea where am I wrong?
任何人都知道我哪里错了?
I also tried to recover the data with jquery but with no positive results.
我还试图用jquery恢复数据但没有积极的结果。
5 个解决方案
#1
0
This should do it.
这应该做到这一点。
$.post("Locali.php",{
// any parameters you want to pass
},function(d){
alert("1");
for (var i=0; i<d.length; i++) {
point = new google.maps.LatLng(d[i].item.latitudine,d[i].item.longitudine);
alert(point);
}
}, 'json');
the PHP is fine if it is giving the response you mentioned above.
如果它给你上面提到的响应,PHP就没问题。
#2
0
I think u should look more for the data you are getting from the php file. Definitely this is a parse error and must be missing some bracket/something else, ultimately not making the data returned to be a json parseable string.
我想你应该从php文件中获取更多数据。肯定这是一个解析错误,必须缺少一些括号/其他东西,最终不会使数据返回为json可解析字符串。
#3
0
You should be Ok with this slight modifications:
你应该对这些稍作修改感到满意:
$items = array();
while($r = mysql_fetch_array($result)) {
$items[] = array(
"id_locale" => $r['id_locale'],
"latitudine" => $r['lat'],
"longitudine" => $r['lng']
);
}
echo json_encode($items);
And the jQuery:
和jQuery:
$.ajax({
type:"POST",
dataType: 'json',
url:"Locali.php",
success:function(data){
console.log(data);
for (var i=0; i<data.length; i++) {
point = new google.maps.LatLng(data[i].item.latitudine,data[i].item.longitudine);
alert(point);
}
}
})
#4
0
data $.ajax({ type:"POST", dataType: json, url:"Locali.php", success:function(data){ for (i in data) { point = new google.maps.LatLng(json[i].item.latitudine,json[i].item.longitudine); alert(point);
} } })
data $ .ajax({type:“POST”,dataType:json,url:“Locali.php”,success:function(data){for(i in data){point = new google.maps.LatLng(json [i ] .item.latitudine,json [i] .item.longitudine); alert(point);}}})
Try it like that.
试试吧。
#5
-1
yeah just try
是的,试试吧
for (var i=0; i<json[0].length; i++) {
cause you have an object there..
因为那里有一个物体..
#1
0
This should do it.
这应该做到这一点。
$.post("Locali.php",{
// any parameters you want to pass
},function(d){
alert("1");
for (var i=0; i<d.length; i++) {
point = new google.maps.LatLng(d[i].item.latitudine,d[i].item.longitudine);
alert(point);
}
}, 'json');
the PHP is fine if it is giving the response you mentioned above.
如果它给你上面提到的响应,PHP就没问题。
#2
0
I think u should look more for the data you are getting from the php file. Definitely this is a parse error and must be missing some bracket/something else, ultimately not making the data returned to be a json parseable string.
我想你应该从php文件中获取更多数据。肯定这是一个解析错误,必须缺少一些括号/其他东西,最终不会使数据返回为json可解析字符串。
#3
0
You should be Ok with this slight modifications:
你应该对这些稍作修改感到满意:
$items = array();
while($r = mysql_fetch_array($result)) {
$items[] = array(
"id_locale" => $r['id_locale'],
"latitudine" => $r['lat'],
"longitudine" => $r['lng']
);
}
echo json_encode($items);
And the jQuery:
和jQuery:
$.ajax({
type:"POST",
dataType: 'json',
url:"Locali.php",
success:function(data){
console.log(data);
for (var i=0; i<data.length; i++) {
point = new google.maps.LatLng(data[i].item.latitudine,data[i].item.longitudine);
alert(point);
}
}
})
#4
0
data $.ajax({ type:"POST", dataType: json, url:"Locali.php", success:function(data){ for (i in data) { point = new google.maps.LatLng(json[i].item.latitudine,json[i].item.longitudine); alert(point);
} } })
data $ .ajax({type:“POST”,dataType:json,url:“Locali.php”,success:function(data){for(i in data){point = new google.maps.LatLng(json [i ] .item.latitudine,json [i] .item.longitudine); alert(point);}}})
Try it like that.
试试吧。
#5
-1
yeah just try
是的,试试吧
for (var i=0; i<json[0].length; i++) {
cause you have an object there..
因为那里有一个物体..