簡體   English   中英

如何從$(document).ready(function())調用javascript函數

[英]How to call javascript function from the $(document).ready(function())

如您在下面看到的,我試圖在文檔加載后調用這兩個函數,但是當我運行代碼時,出現以下錯誤: syntax error unexpected token: <

編輯:我在下面添加了我的html代碼,以便您可以仔細看一下。 謝謝

這是我的index.php文件:

<?php
require 'fb-php-sdk/facebook.php';
$app_id = '251156051648844';
$app_secret = 'be37135d238e66ddce8f4ce8db20e668';
$app_namespace = '';
$app_url = 'http://www.paulmeskers.com/rutgers/twenty/canvas/';
$scope = 'email,publish_actions';

// Init the Facebook SDK
$facebook = new Facebook(array(
'appId'  => $app_id,
'secret' => $app_secret,
));

// Get the current user
$user = $facebook->getUser();

// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
  'scope' => $scope,
  'redirect_uri' => $app_url,
));

print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}

if(isset($_REQUEST['request_ids'])) {
  $requestIDs = explode(',' , $_REQUEST['request_ids']);
  foreach($requestIDs as $requestID) {
  try {
    $delete_success = $facebook->api('/' . $requestID, 'DELETE');
   } catch(FacebookAPIException $e) {
    error_log($e);
    }
  }
}



?>

<!DOCTYPE html>

<html>
<head>
<title>Twenty Questions</title>
<meta property="og:title" content="ttt" />
<meta property="og:type" content="activity" />
<meta property="og:url" content="https://apps.facebook.com/251156051648844/" />
</head>
<body id="yo">
<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>
<script type='text/javascript' src='jquery-1.7.2.min.js'></script>
<script type='text/javascript' src='ui.js'></script>

<script>
    var appId = '<?php echo $facebook->getAppID() ?>';
    var uid;

    // Initialize the JS SDK
    FB.init({
        appId: appId,
        cookie: true,
    });

    FB.getLoginStatus(function(response){
        uid = response.authResponse.userID ? response.authResponse.userID: null;
    });

function authUser() {
    FB.login(function(response) {
    uid = response.authResponse.userID ? response.authResponse.userID : null;
    }, {scope:'email,publish_actions'});

}


</script>


    <input type="button" id="b1" value="Create Game" onClick="createGame(uid)"/>
<input type="button" id="b2" value="Share this game" onClick="shareOnTimeline()" />
    <input type="button" id="b4" value="fetch created games" onClick="fetch_creator_games(uid)" />
    <input type="button" id="b5" value="fetch joined games" onClick="fetch_joined_games()"/>
    <input type="button" id="b3" value="tewst" onClick="build_creator_ui()" />

    <p><b>Games created by me:</b></p>
    <div id="createdGames" ></div>
    <p>------------------------------------------------------</p>
    <p><b>Games created by others:</b></p>
    <div id="invitedGames" ></div>



</body>
</html>

頁面執行期間的某個地方, <字符會出現在不應出現的位置。 由於您建議在jQuery源文件中引發此錯誤,因此您選擇器的編寫可能不正確。 也許您試圖選擇一個HTML元素而忘記將其用引號引起來,或者您試圖創建一個元素。

保持JavaScript干凈

可能由類似於以下內容的錯誤引起:

$(<input>).attr("id", "foo");

請注意,選擇器沒有用引號引起來,因此將引發錯誤,該錯誤的讀取方式類似於您收到的錯誤。

檢查AJAX響應

我注意到您期望JSON順利通過,但是將一些POSTS推送到您的服務器后,我發現錯誤的userid將導致以下響應:

<br/><br/>
<label style='color:red;font-size:200%;'>
  Unable to load guessing games for user_id
</label>
<br/>

這可能會引起問題。 確保將錯誤消息也向下發送為JSON。

{ success: 0, message: 'Unable to load guessing games for this user id' }

要在PHP中構造此JSON字符串,您可以執行以下操作:

$response = array( 'success' => 0, 'message' => 'Unable to...this user id' );
echo json_encode( $response );

保持標記清潔

確保您還有正確的頁面:

<!DOCTYPE html><!-- no whitespace or output before the doctype -->
<html>
  <head>
    <title>Title Here</title>
    <style>
      body { color: red } /* styles in the head */
    </style>
    <script>var scripts_in_the_head_tag = true;</script>
  </head>
  <body>
    <p>HTML goes within the body tags.</p>
    <script>var scripts_in_the_body_tag = true;</script>
  </body>
</html>

當我發出一個期望JSON響應但接收HTML的ajax請求時,通常會看到此錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM