我不是开发人员,但非常擅长复制/粘贴。
我正在尝试在网页 (JavaScript ).如果没有 PHP,这可能吗?如果是这样,你能给我一些示例代码吗?
我一直在寻找,但我找到的所有示例都使用 PHP。
我使用了以下代码:
<!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
// script goes here
$.getJSON('https://latitude.google.com/latitude/apps/badge/api?&user=xxxxxxxxxxxxxxxxxxx', function(data) {
alert(data.type);
});
});
</script>
</body>
</html>
我试过的这段代码出错了:
3 requests ❘ 21.38KB transferred ❘ 470ms (onload: 448ms, DOMContentLoaded: 448ms)
104ms157ms209ms261ms313ms366ms418ms470ms
OPTIONS https://latitude.google.com/latitude/apps/badge/api?&user=xxxxxxxxxxxxxxxx 405 (Method Not Allowed) jquery.min.js:19
o.extend.ajax jquery.min.js:19
o.extend.get jquery.min.js:19
o.extend.getJSON jquery.min.js:19
(anonymous function) json.html:12
o.extend.ready.o.readyList jquery.min.js:19
o.extend.each jquery.min.js:12
o.extend.ready jquery.min.js:19
(anonymous function) jquery.min.js:19
XMLHttpRequest cannot load https://latitude.google.com/latitude/apps/badge/api?&user=xxxxxxxxxxxxxxxxxxxxxx. Origin h ttp://dl.dropbox.com is not allowed by Access-Control-Allow-Origin.
最佳答案
为了发出跨域 AJAX 请求,您需要使用 CORS 或 JSONP。这是服务器必须支持的东西。谷歌纵横似乎不支持这个,所以你需要使用服务器端语言(如PHP)来获取数据。
编辑:如果您不想使用 PHP,而只想使用 JavaScript,您可以使用 Yahoo's YQL (您可能需要一个 API key )。
var googleURL = 'https://latitude.google.com/latitude/apps/badge/api?user=xxxxxxxxxxxxxxxxx&type=json';
$.getJSON('http://query.yahooapis.com/v1/public/yql?callback=?', {
q: 'select * from json where url="'+googleURL+'"',
format: 'json'
}, function(data) {
if(data.query.count){
var gData = data.query.results.json;
alert(gData.type);
}
});
关于javascript - 在 Javascript 中解析 Google Latitude JSON(没有 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590842/