jQuery json.text?
我正在尝试做一个连接页面,您可以在其中查看 twitter、facebook、youtube 等的所有内容...我正在使用 jquery 中的 getJson 函数从 twitterApi 获取数据:http://api.twitter.com/1/users/show.json?screen_name=Someusername。我遇到的问题是,我可以获取返回的 json 文件中的所有数据除了 text 字符串,其中包含最新的帖子。当我尝试获取 json.text 时,它返回 undefined,这是不正确的,因为我之前直接通过浏览器访问过返回的 json。我认为默认的 jQuery.text() 函数弄乱了它,但我没有任何证据。如果是这样,有人知道解决方法吗?
代码(twitter.php只是一个php文件,它从twitter站点复制数据以避免跨域脚本问题):
function setUser() {
$('#intro').fadeOut();
$('body').css('background-color', "#" + bg);
$('body').css('background-image', 'url(' + bgImg + ')');
$('body').css('background-repeat', 'repeat');
$('.imgwrap').html('<img class="profimg" src="' + ppic + '"/>');
$('.username').text(username);
}
var bg = "white";
var ppic = "http://test.campatet.com/identicon/ident.php?size=48";
var bgImg = "none";
var username = "undefined";
$(function () {
$('#submitid').click(function () {
$('#login, #submitid, #introwrapper label').hide();
$('#spinner').spin();
$('#intro').css("width", "99%").width("100%");
$.getJSON('twitter.php?f=show&p="1/users/"&q="screen_name=' + $('#login').val() + '"', function (json) {
bg = json.profile_background_color;
alert(json.text);
if (json.profile_image_url) {
ppic = json.profile_image_url;
}
if (json.profile_background_image_url) {
bgImg = json.profile_background_image_url;
}
if (json.error) {
username = json.error;
$('#introwrapper').html('Error: ' + username + '. <a href="javascript:location.reload(true);" style="color:white;">Refresh Page</a>');
} else {
username = json.name;
setUser();
}
});
});
});
I'm trying to do a connect page where you can view everything for twitter, facebook, youtube, ... I'm using the getJson function in jquery to get the data from the twitterApi:http://api.twitter.com/1/users/show.json?screen_name=Someusername. The issue that I'm having is that I can get all the data in the returned json file except for the text string, which contains the latest post. When I try to get json.text, it returns undefined, which is not true because I have visited the returned json straight through my browser before. I think that the default jQuery.text() function is messing with it, but I don't have any proof. If that's how it is, does anyone know a work around?
Code (twitter.php is just a php file that copies the data from the twitter site to avoid cross domain scripting issues):
function setUser() {
$('#intro').fadeOut();
$('body').css('background-color', "#" + bg);
$('body').css('background-image', 'url(' + bgImg + ')');
$('body').css('background-repeat', 'repeat');
$('.imgwrap').html('<img class="profimg" src="' + ppic + '"/>');
$('.username').text(username);
}
var bg = "white";
var ppic = "http://test.campatet.com/identicon/ident.php?size=48";
var bgImg = "none";
var username = "undefined";
$(function () {
$('#submitid').click(function () {
$('#login, #submitid, #introwrapper label').hide();
$('#spinner').spin();
$('#intro').css("width", "99%").width("100%");
$.getJSON('twitter.php?f=show&p="1/users/"&q="screen_name=' + $('#login').val() + '"', function (json) {
bg = json.profile_background_color;
alert(json.text);
if (json.profile_image_url) {
ppic = json.profile_image_url;
}
if (json.profile_background_image_url) {
bgImg = json.profile_background_image_url;
}
if (json.error) {
username = json.error;
$('#introwrapper').html('Error: ' + username + '. <a href="javascript:location.reload(true);" style="color:white;">Refresh Page</a>');
} else {
username = json.name;
setUser();
}
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查响应 JSON 字符串后,我得出结论,您必须使用
json.status.text
而不是json.text
。请参阅下面的响应 JSON 结构:
Fiddle: http://jsfiddle.net/jmwy4/
还可以使用 http://jsbeautifier.org/ 缩进 JSON 响应,以获得清晰的视图返回 JSON 对象/响应。
After inspecting the response JSON string, I've concluded that you have to use
json.status.text
instead ofjson.text
.See below for the structure of the response JSON:
Fiddle: http://jsfiddle.net/jmwy4/
It's also possible to use http://jsbeautifier.org/ for indenting a JSON response, to get a clear view of the returned JSON object/response.