Javascript 访问JSON';将对象值发送到另一个servlet或jsp
我不熟悉使用Facebook应用程序环境。我正在开发一个小应用程序。我试图在我的项目中使用一些社会数据来检查一些条件。 我不知道我是否以正确的方式访问FB返回的JSON对象,但这是我的问题 我想将firstname从当前文件发送到另一个jsp或servlet。因此,我创建了jsp变量,并尝试在从Facebook获得的响应对象中分配'name'的值 当试图在jsp文件末尾打印firstname时,firstname输出为空。如何在对象中使用名称和性别值并将其发送到另一个jsp文件或servlet文件 我绞尽脑汁想找到解决办法。有人请帮忙Javascript 访问JSON';将对象值发送到另一个servlet或jsp,javascript,facebook,facebook-graph-api,facebook-javascript-sdk,dom-events,Javascript,Facebook,Facebook Graph Api,Facebook Javascript Sdk,Dom Events,我不熟悉使用Facebook应用程序环境。我正在开发一个小应用程序。我试图在我的项目中使用一些社会数据来检查一些条件。 我不知道我是否以正确的方式访问FB返回的JSON对象,但这是我的问题 我想将firstname从当前文件发送到另一个jsp或servlet。因此,我创建了jsp变量,并尝试在从Facebook获得的响应对象中分配'name'的值 当试图在jsp文件末尾打印firstname时,firstname输出为空。如何在对象中使用名称和性别值并将其发送到另一个jsp文件或servlet文
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Facebook authentication usage</title>
</head>
<body>
<%! String firstname="";
String lastname="";
String middlename="lynne";
%>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '532238706811106', // App ID from the App Dashboard
//channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
frictionlessRequests : true,
xfbml : true // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected')
{
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me', function(response)
{
console.log(response);
console.log('Good to see you, ' + response.name + '.');
firstname=firstname+response.name;
lastname=lastname+response.last_name;
document.write(response.name+middlename);
});
}
else if (response.status === 'not_authorized')
{
// the user is logged in to Facebook,
// but has not authenticated your app
var oauth_url = 'https://www.facebook.com/dialog/oauth/';
oauth_url += '?client_id=532238706811106'; //Your Client ID
oauth_url += '&redirect_uri=' + 'https://apps.facebook.com/newjsp'; //Send them here if they're not logged in
//oauth_url += '&scope=user_about_me,email,user_location,user_photos,publish_actions,user_birthday,user_likes';
window.top.location = oauth_url;
}
else
{
// the user isn't logged in to Facebook.
window.top.location ='https://www.facebook.com/index.php';
}
});
};
// Load the SDK's source Asynchronously
// Note that the debug version is being actively developed and might
// contain some type checks that are overly strict.
// Please report such bugs using the bugs tool.
(function(d, debug)
{
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
</script>
<h1>this is a sample to test facebook authentication
Welcome <%=firstname+""+lastname+middlename%></h1>
<%out.print(firstname+""+lastname+""+middlename); %>
</body>
</html>
Facebook身份验证使用情况
window.fbAsyninit=函数(){
//初始化FBJSSDK
FB.init({
appId:'532238706811106',//应用程序仪表板中的应用程序ID
//channelUrl:'//WWW.YOUR_DOMAIN.COM/channel.html',//用于x域通信的通道文件
status:true,//在初始化时检查登录状态?
cookie:true,//设置会话cookie以允许服务器访问会话?
无摩擦要求:正确,
xfbml:true//是否解析此页面上的xfbml标记?
});
//附加的初始化代码(如添加事件侦听器)如下所示
FB.getLoginStatus(函数(响应){
如果(response.status===“已连接”)
{
//用户已登录并已验证您的身份
//app和response.authResponse提供
//用户ID、有效访问令牌、签名
//请求,以及访问令牌的时间
//和签名的请求都将过期
var uid=response.authResponse.userID;
var accessToken=response.authResponse.accessToken;
FB.api('/me',函数(响应)
{
控制台日志(响应);
log(“很高兴见到你,+response.name+”);
firstname=firstname+response.name;
lastname=lastname+response.last\u name;
文件.书写(响应.名称+中间名);
});
}
else if(response.status===“未授权”)
{
//用户已登录到Facebook,
//但尚未验证您的应用程序
var oauth_url='1〕https://www.facebook.com/dialog/oauth/';
oauth_url+='?客户机_id=532238706811106';//您的客户机id
oauth_url+='&重定向_uri='+'https://apps.facebook.com/newjsp“;//如果他们没有登录,请将他们发送到这里
//oauth_url+='&scope=user_关于我、电子邮件、用户位置、用户照片、发布操作、用户生日、用户喜好';
window.top.location=oauth\u url;
}
其他的
{
//用户未登录到Facebook。
window.top.location文件https://www.facebook.com/index.php';
}
});
};
//异步加载SDK的源代码
//请注意,调试版本正在积极开发中,可能
//包含一些过于严格的类型检查。
//请使用bug工具报告此类bug。
(功能(d,调试)
{
var js,id='facebook jssdk',ref=d.getElementsByTagName('script')[0];
if(d.getElementById(id)){return;}
js=d.createElement('script');js.id=id;js.async=true;
js.src=“//connect.facebook.net/en_US/all”+(debug?/debug):“)+”.js”;
ref.parentNode.insertBefore(js,ref);
}(文档,/*debug*/false));
这是一个测试facebook身份验证的示例
欢迎
这是因为您将firstname
设置为”
,但从不更改该值
在JavaScript代码中,firstname=firstname+response.name
不设置先前在Java代码中定义的字符串变量。相反,它设置了一个只有JavaScript代码才能看到的新变量
Java代码在服务器端运行,而JavaScript在客户端运行。它们是两个完全不同的程序,在不同的时间、不同的地点执行