ユーザーが facebook js SDK 経由でログインしているときに jquery から $.post を呼び出すと、問題が発生します。コールバック関数の応答は、php ファイルでエコーされたものではなく、現在のページの html です。ユーザーが Facebook 以外のユーザーとしてログインすると、すべてが正しく機能します。この問題に関するドキュメントが見つからないようで、Facebook のドキュメントを正しくフォローしているように感じますが、明らかに何かが欠けています。
私のプロセスは次のとおりです。1.) ユーザーがログインします (Facebook 経由または直接私のサイトに)。2.) ユーザーが保存リンクをクリックして、ビジネスをプロファイルに保存します。3.) リンクの保存では、jquery を使用して $.post リクエストを php ファイルに実行し、データベースを更新します。4.) php は、成功の場合は「1」、失敗の場合は「0」を返します。5.) jquery はユーザーに失敗を警告するか、成功時にボタンを無効にします。
HTML
<!DOCTYPE html>
<html xmlns:fb="http://ogp.me/ns/fb#">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex, nofollow" />
<!-- scripts -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="scripts/pagejs.js"></script>
<!-- end scripts -->
<title><? echo $pageTitle; ?></title>
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=999999999999999";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<header>
... page html...
<a href='javascript:saveBiz(64)' class='grey-button' title='Save business and add it to your profile.' id='save64'><img src='images/save-small.png' alt='' width='10' height='10' />Save</a>
... rest of page ...
pagejs.js
$(function() {
$.ajaxSetup({ cache: true });
$.getScript('//connect.facebook.net/en_US/all.js', function(){
window.fbAsyncInit = function() {
FB.init({
appId: '999999999999999',
channelUrl: '//www.domain.com/channel.html',
status: true,
cookie: true,
xfnml: true
});
$('#loginbutton,#feedbutton').removeAttr('disabled');
FB.getLoginStatus(updateStatusCallback);
};
});
});
function saveBiz(bizID) {
submitAction = "save";
$.post("updates.php", { businessID : bizID, action: submitAction },
function(responseData){
if(responseData == "0"){
alert('An error occurred while saving the business. Please try again. If the problem persists, please contact us.');
}
else {
alert(responseData); // for testing purposes
$('#save'+bizID).fadeTo("fast", .5).removeAttr("href");
$('#save'+bizID).text('Saved');
$('#save'+bizID).attr('title','Business saved.');
}
},
"text"
);
}
update.php
<?php
require_once 'includes/classes/customer.class.php';
session_start();
$custID = $_SESSION['customerID'];
$action = $_POST['action'];
$bizID = $_POST['businessID'];
$returnVal= '0';
if($custID && $bizID && $action == 'save') {
$returnVal = customers::saveUserBusiness($custID, $bizID);
}
elseif($custID && $bizID && $action == 'remove') {
$returnVal = customers::removeUserBusiness($custID, $bizID);
}
echo $returnVal;
?>
よろしくお願いいたします。問題の診断に役立つものが上記にない場合はお知らせください。