[英]I'm trying to use Ajax to capture facebook data and write it to a text file named test.txt
我遵循了此頁面上的第二個答案,除了我用一個名為test.txt的簡單空文本文件替換了看上去很復雜的php部分。 我沒有收到任何錯誤消息,但文件仍然為空(我似乎無法將數據寫入文件;有人知道為什么嗎?更好的是,我想直接將數據寫入到mongodb數據庫! 使用Facebook登錄Javascript SDK獲取用戶數據
我的代碼如下:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '753236104711327',
status : true,
xfbml : true
});
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log(response);
$.ajax(
{
type : 'POST',
data : response, //all data
url : 'test.txt',
success : function()
{
console.log(response.name)
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'user_birthday, friends_birthday, basic_info, user_about_me, friends_about_me, user_education_history, friends_education_history, user_hometown, friends_hometown, user_interests, friends_interests, user_location, friends_location, user_questions, friends_questions, user_relationships, friends_relationships, user_relationship_details, friends_relationship_details, user_religion_politics, friends_religion_politics, user_work_history, friends_work_history'});
};
(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";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
任何幫助將不勝感激!
您在這里所做的是將POST請求發送到文本文件,這沒有任何意義,除非您已設置服務器來捕獲該請求。
data:
$.ajax()
調用的一部分將給定的對象(在本例中為response
對象)轉換為查詢字符串,然后將其作為請求的一部分發送。
幾乎就像您在地址欄中輸入以下內容一樣:
http://your.website.net/path/to/test.txt?responseProperty1=someData&responseProperty2=someOtherData
並且test.txt
沒有任何處理查詢字符串的方法,因為它只是一個空文本文件。
您可能將不得不使用服務器端語言(如PHP)將數據寫入文件中。 然后,您將ajax請求發送到服務器上能夠處理請求參數的文件:
$.ajax({
type: 'POST',
data: response,
url: 'handle_response.php',
success: function() {
// it worked
}
});
我不太擅長PHP,也許您還是願意使用其他方式,所以我將其保留在那里,但是我希望這會有所幫助
您無法使用Javascript將數據寫入文本文件。 如果要將數據寫入文本文件或Mongo,則需要將其發送到服務器,並讓服務器處理文件或數據庫的寫入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.