javascript - 在 Javascript 中解析 Google Latitude JSON(没有 PHP)

标签 javascript json google-latitude

我不是开发人员,但非常擅长复制/粘贴。

我正在尝试在网页 (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/

相关文章:

java - 什么是在 JPEG(EXIF 格式)上编写 GPS 元数据的好 Java 库?

javascript - 使用 Apps 脚本通过 HTTP API 执行 Mixpanel 的 JQL 脚本时出现输入错误结束

javascript - DOM - 一页上超过 5 个 'window' 对象

javascript - 脚本5009 : 'JSON' is undefined

wpf - 来自 WPF 桌面应用程序的 OAuth 2.0 (Google API) 身份验证

geolocation - 模拟 GPS 或串行设备

javascript - 从 Json 格式的 Javascript 获取数组值

javascript - 你将如何制作波浪动画 div css/js

python - 如果键属性不存在,则解析 json 文件时获取 0 条记录

json - 从 Guidebox API 在 Swift 中解析 JSON 数据