Skip to content

Instantly share code, notes, and snippets.

@dominicgan
Created August 11, 2016 08:16
Show Gist options
  • Save dominicgan/1a55ebf2b2d2249e70527ea7c579303d to your computer and use it in GitHub Desktop.
Save dominicgan/1a55ebf2b2d2249e70527ea7c579303d to your computer and use it in GitHub Desktop.
Publish photo to Facebook (jQuery Ajax - POST) - August 2016
var pgToke = 'YOUR_PAGE_TOKEN_HERE'; // generate page token with GET or in Facebook graph explorer
var caption = document.getElementById('post_status').value;
var fileUl = document.getElementById('post_img');
file = fileUl.files[0];
// Create the FormData object and attach to a key name "file"
var fd = new FormData();
fd.append("file", file);
fd.append("caption", caption);
// ajax method to POST photo to Facebook page
$.ajax({
type: "POST",
url: "https://graph.facebook.com/v2.7/YOUR_PAGE_ID_HERE/photos?access_token="+pgToke,
data: fd,
contentType: false,
processData: false,
success: function(dat, status, xhr){
console.log(xhr.responseText);
},
error: function(xhr, str, err){
console.log(xhr.responseText);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment