0

我正在尝试将使用 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");
});
4

1 回答 1

1

如果您还没有这样做,请使用 Chrome(或 FireFox、IE 等)开发人员工具,重新加载页面或 Ajax 调用,然后查看“控制台”和“网络”选项卡(或它们的等效选项卡)以查看错误更具体。

$(this).text(msg)但是,鉴于您收到的错误消息() ,我很确定您的错误可能出现在您编写成功消息 () 的 Javascript 部分...'createDocumentFragment' of undefined

因此,与此同时,您可以尝试在成功函数中对 DOM 对象 id/class 进行硬编码,在该函数中将消息写入页面并查看它是否有效,如下所示:

<div id="myDiv"></div>

     $.ajax({

                    type: "GET",
                    url: "fql.php",
                    data: {'first_name': first_name, 'last_name': last_name},
                    dataType: "text",
                    success: function(msg){
                        $("#myDiv").text(msg);
                    }

        });

另外,请注意我在dataType上面明确设置。虽然并不总是需要,但最好告诉 jQuery 它将从服务器接收什么样的响应类型。

于 2013-07-04T00:12:09.580 回答