No callback passed to the ApiClient - javascript

When I try to perform an action with JS FB.api([...], I get No callback passed to the ApiClient [...]
Where can I set this callback?

Just add a callback function to the API call:
FB.api(
'/me/[YOUR_APP_NAMESPACE]:[YOUR_ACTION]',
'post',
{ recipe: '[LINK_TO_YOUR_OBJECT]' },
function(response) {
if (!response || response.error) {
alert('Error occured');
}
else {
alert('Action was successful! Action ID: ' + response.id)
}
}
);

Related

Calling sync ready made async ajax javascript function

I want to call this function on button click after login and wait for result, to get token value. This function cannot be changed, it is async and supplied from other currently unavailable team.
I already tried something like this, but with no success. I get web service results, but I can't write appropriate sync call to wait to return token.
function getToken() {
param1 = "123456";
ajax_oauth(param1, function (success, response) {
success: return response.token;
});
}
function ajax_oauth(param1, callback) {
APP.debug("oauth login with param1 " + param1);
try {
APP.blockUI();
var DeviceID = APP.readRegistry(APP_CONFIG.REGISTRY.DeviceID);
//---------------------------------------------------------------
$.ajax(
auth_token_url,
{
method: "GET",
accept: 'application/json',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
'param1': param1,
'deviceId': DeviceID
}),
xhrFields: {
withCredentials: false
},
statusCode: {
201: function (response) {
APP_STATE.hasOauth = true;
APP.debug('got response 200 from oauth');
auth.login(response.token); //TODO read expiration from token
try {
var decoded = jwt_decode(response.token);
APP_STATE.uid = decoded.uid;
} catch (err) {
APP.error("unable to decode token " + JSON.stringify(err));
}
},
401: function () {
},
500: function () {
},
503: function () {
}
},
success: function (response) {
APP.unblockUI();
APP_STATE.restAvailable = true;
},
error: function (jqXHR, textStatus, errorThrown) {
APP.unblockUI();
APP_STATE.restAvailable = false;
APP.restError(auth_token_url, jqXHR, errorThrown, textStatus);
APP.callback(callback, false);
}
}
);
} catch (err) {
APP.error("unable to do oauth login, " + err);
}
};
After user clicks on login button, I want to call function ajax_oauth and to return token if params ok. If not, to return login error. Login can't be async, as far as I can see.
For whatever reason you can't tap into the original ajax response, you could intercept the request using $.ajaxPrefilter.
From your code it looks like auth_token_url has a global reference. You could use this to intercept the call by matching the outgoing request on the resource URL.
$.ajaxPrefilter('json', function(options, originalOptions, jqXHR) {
if (options.url === auth_token_url) {
jqXHR.done(function(response) {
try {
var decoded = jwt_decode(response.token);
console.log(decoded);
} catch (err) {
APP.error("unable to decode token " + JSON.stringify(err));
}
});
}
});
Note that this needs to be declared well before the request is made preferably after jQuery is loaded.

Post a message as a page using JAVASCRIPT

I had successfully post a message to my own Facebook page but it was posted as a visitor(I want post as a page). I'm not sure where i missed and also i confused to the page access token.
$('#btn-fb').click(function () {
getPageAccess(function (page_id){
postToFB(page_id,'sien');
});
//postStatus();
});
function getPageAccess(callback) {
FB.login(function (response) {
// handle the response
if (response.authResponse) {
FB.api('/' + page_id + '', {fields: 'access_token'}, function (response) {
if (response && !response.error) {
callback(page_id);
}
});
}
}, {scope: 'manage_pages,publish_pages'});
}
function postToFB(page_id, message) {
FB.api(
"/" + page_id + "/feed",
"post",
{
"message": message
},
function (response) {
if (response && !response.error) {
/* handle the result */
}
else {
}
}
);
}
You need to use a Page Token to post as Page, right now you are using a User Token. You are asking for the Page Token (fields parameter), but you never use it and you don´t add it as callback parameter:
{
'message': message,
'access_token': pageToken
}

How to wait for the facebook javascript api response using javascript or jquery

I want my my postuserWall() function to wait for the FB.api response then it return true or false.but it is not waiting for the response.
How can i get it to wait for the response
here is my main function which is calling the postuserWall function the alert is showing me undefined
$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
e.preventDefault();
var $this = $(this);
var chcek = postuserWall();
alert(chcek);
if(check){
window.location = $this.attr('href');
}
});
Here is my postuserWall function
function postuserWall(){
var body = 'Usama New Post';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
console.log(response);
alert('Error occured');
return false;
} else {
alert('Post ID: ' + response.id);
return true;
}
});
}
it is not waiting for the response in the postuserWall function
One solution (make sure you understand what asynchronous means, it´s very important in JavaScript):
$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
e.preventDefault();
var $this = $(this);
postUserWall(function(check) {
alert(check);
if(check){
window.location = $this.attr('href');
}
});
});
function postUserWall(callback) {
var body = 'Usama New Post';
FB.api('/me/feed', 'post', {message: body}, function(response) {
if (!response || response.error) {
console.log(response);
alert('Error occured');
callback(false);
} else {
alert('Post ID: ' + response.id);
callback(true);
}
});
}
You can also use Promises, but that´s overkill for just one callback.

How use the success call back in a delete ajax [NODE.JS]

I'm using the following code to delete a collection in my db:
Client:
$('.destroy').click(function() {
if(confirm("Are u sure?")) {
$.ajax({
type: 'DELETE',
url: '/destroy/' + dataId,
success: function(response) {
console.log('Success');
}
});
} else {
alert('Cancelled');
}
});
Server:
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
console.log(err)
}
else {
console.log('Collection removed!');
}
});
});
Is working, if i click in the destroy button and reload the page, the collection will not be there, but the success callback function with the: [console.log('Success');] is not running..
I need send a callback from the server to the client ir order to make the success function run???
How make the console.log('Success'); run??
Thanks.
The ajax call probably just times out, as it's never getting a response back from the server.
Send a response from the server
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
Then catch it
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
This is a simplified example, you can return whatever you like, send statusCodes or whatever.

Delete facebook post from page

Am working on a facebook application to post images to a page, i want to delete a particular the facebook post using javascript api, any help please?
This is my code:
FB.api('/1234/photos', 'post', {
access_token : FB.getAuthResponse()['accessToken'],
message: "message",
url : '',
from : 1234,
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Delete post function:
function delete_post(post_id)
{
FB.api(post_id, 'delete',function(response) {
console.log(response)
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post was deleted');
}
});
}
This is not working for me, always getting error occured message.
which Id should i choose to delete a post? response.id or response.post_id?
Thanks

Categories

Resources