Php 使用jQuery检索远程JSON内容
在Jquery脚本中检索Json有问题 一个php脚本,位于echo的return处,类似于JSON:Php 使用jQuery检索远程JSON内容,php,jquery,json,Php,Jquery,Json,在Jquery脚本中检索Json有问题 一个php脚本,位于echo的return处,类似于JSON: {"locations":[{"name":18492554,"lat":"12345","long":"234"},{"name":18492553,"lat":"4567","long":"234},{"name":18492555,"lat":"2234","long":"234}]} 我想在我的Jqueru脚本中描绘这一点,如: (function() { window.on
{"locations":[{"name":18492554,"lat":"12345","long":"234"},{"name":18492553,"lat":"4567","long":"234},{"name":18492555,"lat":"2234","long":"234}]}
我想在我的Jqueru脚本中描绘这一点,如:
(function() {
window.onload = function() {
// Creating a new map
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(41.6, -0.88),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
///////////////////// GET the JSON data ///////////////////
var json = // ???????
// Creating a global infoWindow object that will be reused by all markers
var infoWindow = new google.maps.InfoWindow();
// Looping through the JSON data
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.long);
// Creating a marker and putting it on the map
//var iconBase = 'https://dea-srl.net/domenico/traking/js/';
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.nombre,
icon: iconBase + 'schools_maps.png'
});
// Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
(function(marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.nombre);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
})();
但它不起作用
你知道吗?提前谢谢。你说的什么不起作用?你有什么错误 您是否正在使用
done
功能
$.get(" http://myserver.com/script.php").done(function(data) {
console.log(data);
});
或者您可以直接使用该函数
加载json数据。
如果有任何其他问题,你必须更精确一点。你说的“它不工作”是什么意思?会发生什么?您是否收到错误?我不知道如何调试,但我稍后尝试放置analert,alert(json),但没有显示。我怎么能得到一个错误?是你的脚本的同一个域吗?你的程序的控制台放:ReferenceError:$未定义$.get({添加库我的错!虽然我的问题是关于get方法的!非常感谢!
$.get(" http://myserver.com/script.php").done(function(data) {
console.log(data);
});
$.getJSON('http://myserver.com/script.php', function(data) {
var json = data;
});