我正在尝试将使用 ajax 的 javascript 文件中的名称传递给 php 文件,该文件又会返回用户的关于我的文本。出于某种原因,我不断收到错误
Uncaught TypeError: Cannot call method 'createDocumentFragment' of undefined
,并且似乎没有返回 php 文件中的文本。我对 ajax 完全陌生,因此我们将不胜感激。
这是我的 php 文件,底部的 fql 查询实际上应该只是 realavent 但我不知道 facebook 交互的其他必要代码是否与错误有关
<?php
$app_id = 'APPID';
$app_secret = 'APP_SECRET';
$my_url = 'MY_URL';
$code = $_REQUEST["code"];
// auth user
if(empty($code)) {
$dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url) ;
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
// get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url)
. '&client_secret=' . $app_secret
. '&code=' . $code;
// response is of the format "access_token=AAAC..."
$access_token = substr(file_get_contents($token_url), 13);
$graph_url = "https://graph.facebook.com/me?"
. "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
//Check for errors
if ($decoded_response->error) {
// check to see if this is an oAuth error:
if ($decoded_response->error->type== "OAuthException") {
// Retrieving a valid access token.
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url
. "'</script>");
}
else {
echo "other error has happened";
}
}
else {
// success
echo("success" . $decoded_response->name);
echo($access_token);
}
// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes. In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
//Gets the users about me using fql
if($_GET['first_name'] && $_GET['last_name']){
$last_name;
$fql_start = 'https://graph.facebook.com/fql?q=';
$fql_query= 'SELECT about_me FROM user WHERE first_name = "'. $_GET['first_name'] . '" and last_name="'. $_GET['last_name'] . '" and uid in (select uid2 from friend where uid1 = me())';
$fql_end='&access_token=' . $access_token;
$fql_query = urlencode($fql_query);
$fql = file_get_contents($fql_start.$fql_query.$fql_end);
$fql = json_decode($fql, true);
$about_me= $fql['data'][0][about_me];
echo $about_me;
}
?>
这是我的小得多的 javascript 文件,它从标题中获取用户的名称并将该名称传递给 php 文件,以期获得 user_about_me 文本作为回报。
$('.btn').on('click', function (e) {
e.preventDefault(); // to prevent the default behavior of anchor a click from redirecting.
var name = $(this).closest('.span4').find('h3').text();//Get the name of a user from the header
name = name.split;
var first_name = name[0];
var last_name = name[1];
var button = $(this);
$.ajax({
type: "GET",
url: "fql.php",
dataType: "text",
data: {'first_name': first_name, 'last_name': last_name},
success: function(msg){
$(button).text(msg);
}
});
$(this).effect("slide", "normal");
});