I am creating a facebook app and it uses Javascript API to store username and other details in js variables. And this app is created under Python(using Google App Engine). I want to access the js variables in Python and store the values in Db. How to do this?
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
<head>
<title>Captain Pandey</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '185318768251247',
channelURL : 'http://localhost:8080/channel.html',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
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(apiResponse) {
var name = apiResponse.name;
});
}
else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
}
else {
// the user isn't logged in to Facebook.
}
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1" scope="email">Login to Facebook</div>
</body>
</html>
""")
and template used is webapp2
asked Mar 31, 2012 at 5:40
subhojit777
5963 gold badges11 silver badges29 bronze badges
-
3JS variables, as you know, exist on the client side, Python code exists on the server side. The only way to communicate for them is using HTTP (e.g. AJAX). No direct access.bereal– bereal2012年03月31日 05:53:20 +00:00Commented Mar 31, 2012 at 5:53
-
It would help to mark up the code to show which variables in the Javascript you want to store, and at what point, with what controlling logic.Karl Knechtel– Karl Knechtel2012年03月31日 07:47:24 +00:00Commented Mar 31, 2012 at 7:47
1 Answer 1
Design the Javascript code so that it makes an AJAX request that sends the data, and write a Python handler for that request.
answered Mar 31, 2012 at 7:47
Karl Knechtel
61.5k14 gold badges134 silver badges194 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default