2013-04-08 16 views
0

快速背景:我創建了一個基本的Facebook應用程序,以便用戶可以「喜歡」來自Facebook之外的網站的信息。用戶進行身份驗證,被重定向到他們的access_token,這是由某些JS收集的,並通過隱藏字段插入到表單中,並傳遞給POST請求。 (不一定相關,但我也使用一些JS在用戶從Facebook重定向到站點後自動提交'like'。)提交POST請求到Facebook圖表後可能會停止重定向嗎?

在HTTP請求被髮送(並且成功)之後,用戶被重定向到響應頁面,只在屏幕上顯示「真」。有沒有辦法阻止這個重定向?一旦表單被提交,我已經設置了一些JS來替換一些div的內容,但我似乎無法阻止它們被引導。

在此先感謝您的任何想法。

HTML摘錄

<!-- *Authentication Section* --> 
    <div id="submit_like"> 
     <form> 
     <a id="facebook-authentication" href="https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=[MY_APP_ID]&redirect_uri=[MY_POST-AUTHORIZATION_REDIRECT_URL]&scope=offline_access,publish_stream"> 
     <div id="facebook-authentication-button"> 
     START THE VOTE! 
     </div> 
     </a> 
     </form> 
    </div> 

<!-- After Authentication, this form is submitted --> 
<form id="likeform" method="POST" action="https://graph.facebook.com/[FACEBOOK_ID]/likes" > 
    <input id="AuthCode" name="access_token" type="hidden" value="123"> 
</form> 
+0

是否有任何理由不能使用PHP? – phwd 2013-04-08 22:21:30

+0

@phwd是的 - 很簡單。我不知道/使用PHP。 :) – justinraczak 2013-04-09 04:20:48

回答

1

您可以使用jQuery的.post()方法提交的形式,無任何頁面重定向。你的情況:

$.post("https://graph.facebook.com/[FACEBOOK_ID]/likes", $("#likeform").serialize()).done(function(data){ 

    // redirect to new page or do nothing to stay put. 
    alert(data); //Show the returned value 

}); 

.serialize()輔助函數的形式爲輸入並創建URL編碼符號的字符串。該字符串作爲參數發送,其中將包含access_token隱藏表單輸入。

就像提醒一樣,一定要包含jQuery。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>