简体   繁体   English

使用getJSON到达JSON中的数据

[英]Reaching a data in json using getJSON

I want to get the username's profile image. 我想获取用户名的个人资料图片。 So i prefer to use twitter api version 1 for this.(The regular version of api is here ). 所以我更喜欢使用twitter api版本1.(常规api版本在这里 )。 But my code doesn't return any data. 但是我的代码不返回任何数据。 How can i fix this? 我怎样才能解决这个问题?

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(document).ready( function() {   
var userPage = "https//twitter.com/jack"; 
var arr = userPage.split("/");
var username = "";

for(i=3;i<4;i++)
username += arr[i];

var page = 'https://api.twitter.com/1/users/show.json?screen_name='+username;

        $.getJSON(page, function(data) {
            alert(data.profile_image_url);
        });

})

</script>
</head>

<body>
</body>

</html>

Add "&callback=?" 添加"&callback=?" to the URL to force jsonp format to get around the Access-Control-Allow-Origin issue. URL以强制jsonp格式解决Access-Control-Allow-Origin问题。

var page = 'https://api.twitter.com/1/users/show.json?screen_name='+username + "&callback=?";

EXAMPLE

JSONP : JSONP

The way JSONP works is simple, but requires a little bit of server-side cooperation. JSONP的工作方式很简单,但需要一点服务器端合作。 Basically, the idea is that you let the client decide on a small chunk of arbitrary text to prepend to the JSON document, and you wrap it in parentheses to create a valid JavaScript document (and possibly a valid function call). 基本上,这个想法是让客户端决定一小段任意文本以放在JSON文档之前,然后将其包装在括号中以创建有效的JavaScript文档(可能还包括有效的函数调用)。

The client decides on the arbitrary prepended text by using a query argument named jsonp with the text to prepend. 客户端使用名为jsonp的查询参数和要添加的文本来决定任意的添加文本。 Simple! 简单! With an empty jsonp argument, the result document is simply JSON wrapped in parentheses. 使用一个空的jsonp参数,结果文档只是用括号括起来的JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM