jQuery SVG映射-无法从jQuery Ajax将内容作为JSON提取 [英] jQuery SVG map - pulling content as JSON from jQuery Ajax not working
本文介绍了jQuery SVG映射-无法从jQuery Ajax将内容作为JSON提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的代码:
$.ajax({
type: "GET",
url: "content/content.json",
success: function( data ) {
check = true;
ajaxMap = data;
drawMap(data);
}
});
function drawMap( data ) {
$('#map').mapSvg({
source: 'maps/test.svg',
colors: {
selected: "#00431e",
disabled: "#ffffff"
},
tooltipsMode: 'custom',
zoom: true,
zoomButtons: {'show': true, 'location': 'left'},
pan: true,
responsive: true,
zoomLimit: [0,500],
marks: data, // here I pass data from JSON file
tooltipsMode: 'custom',
zoom: true,
pan: true,
responsive: 0,
zoomLimit: [0,100]
});
}
当我直接在标记中以JSON格式传递数据时,它可以工作,但是当我传递数据变量时,它不起作用.
It works when I pass data as JSON format directly in marks, but when I pass data variable it doesn't work.
已更新: 这是我的json数据:
Updated: This my json data:
[
{ c: [50.84199288,122.83167],
attrs: {'src': 'markers/pin-yellow.png'},
tooltip: '<h2>Dublin - Ireland</h2><p>Embassies and High Commissions</p><a href="http://www.dfat.gov.au/geo/ireland/" title="http://www.dfat.gov.au/geo/ireland/">Read More</a>'
},
{ c: [44.94199288,119.93167],
attrs: {'src': 'markers/pin-yellow.png'},
tooltip: '<h2>London - United Kingdom</h2><p>Embassies and High Commissions</p><a href="http://www.dfat.gov.au/geo/united_kingdom/" title="http://www.dfat.gov.au/geo/united_kingdom/">Read More</a>'
},
{ c: [28.94199288,100.93167],
attrs: {'src': 'markers/pin-yellow.png'},
tooltip: '<h2>Header</h2><p>Embassies and High Commissions</p><a href="#" title="">Read More</a>'
},
{ c: [44.94199288,115.93167],
attrs: {'src': 'markers/pin-yellow.png'},
tooltip: '<h2>Header</h2><p>Embassies and High Commissions</p><a href="#" title="">Read More</a>'
},
{ c: [20.94199288,135.93167],
attrs: {'src': 'markers/pin-yellow.png'},
tooltip: '<h2>Header</h2><p>Embassies and High Commissions</p><a href="#" title="">Read More</a>'
}
]
推荐答案
如果您以json格式表示响应,则必须定义dataType属性.当前您正在以字符串形式获取数据.
You have to define dataType property if you aspect response in json format. currently you are getting data as string.
$.ajax({
type: "GET",
url: "content/content.json",
dataType:'json',
success: function( data ) {
check = true;
ajaxMap = data;
drawMap(data);
}
});
或在成功处理程序中使用data=JSON.parse(data)
.
or use data=JSON.parse(data)
in your success handler.
此外,示例中显示的是对象常量,而不是json.参见 http://json.org/. JSON.parse()将在您的示例中显示错误.
Moreover what you have shown in example is object literal , not an json. see http://json.org/ . JSON.parse() will give error on your example.
这篇关于jQuery SVG映射-无法从jQuery Ajax将内容作为JSON提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文