Posting to Facebook app group via ajax is failing silently - ajax

Thank you in advance for any assistance you maybe able to provide. I'm trying to post from my app to an app group that I have created. Using the code below it completes the request successfully but the data returned is empty. Please tell me what I'm doing wrong. I did also notice that it's not performing a POST but rather a GET.
$.ajax({
type: 'POST',
url: 'https://graph.facebook.com/v2.1/'+g_id+'/feed?access_token='+User["User AccessToken"],
data: JSON.stringify({
message: g_mes
}),
dataType: "jsonp",
success: function(data){
console.log(data);
},
error: function(data1){
console.log(data1);
}
})

You really should use the JavaScript SDK for the API. Here is a basic tutorial: https://developers.facebook.com/docs/javascript/quickstart/v2.1
And this is what the API call would look like:
FB.api('/' + g_id + '/feed', 'post', {message: g_mes}, function(response) {
console.log(response);
});
See Facebook docs for a code example too: https://developers.facebook.com/docs/graph-api/reference/v2.1/group/feed
You donĀ“t even need to worry about the Access Token. You just need to use FB.login with the appropriate permissions in the scope parameter to authorize the user.

Related

Method in Ajax other than POST and GET

I was asked in an interview what are the methods in AJAX other than GET and POST. I googled but I couldn't find any . Could someone please tell me.
HEAD Same as GET but returns only HTTP headers and no document body
PUT Uploads a representation of the specified URI
DELETE Deletes the specified resource
OPTIONS Returns the HTTP methods that the server supports
These are generally used methods
Also see detail about
There are four types of calls you cand do to a REST API GET,POST,PUT and DELETE. PUT should be suposedly used to modify already existing data while DELETE should be used to eliminate data. Still due to convention, PUT and DELETE are barely used. Here are a couple of examples of PUT and DELETE Ajax calls
$.ajax({
url: urlCalltoDeleteResource),
type: 'DELETE',
success: function(data) {
alert('Deleted');
},
error: function(data) {
alert('Not Deleted');
},
});
$.ajax({
url: urlToUpdate,
type: 'PUT',
data: "name=NameToUpdate",
success: function(data) {
alert('Load was performed.');
}
});

Jquery AJAX call requires authentification

I'm trying to use the google chart api in an XPages application.
I'm using the code example given by the documentation : https://developers.google.com/chart/interactive/docs/php_example#exampleusingphphtml-file
I have to replace the call to the php page by a call to an LS agent.
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
So my code goes to :
var jsonData = $.ajax({
url: "http://server/database/agent?openagent",
dataType: "json",
async: false
}).responseText;
On my local domino server, it works fine.
On the production domino server, I get nothing. The chart is not drawn. After debugging the js client side, it seems the ajax call is expecting an authentification even if I had to log in before.
The anonymous access is not allowed on both servers.
The security level seems to be same on both environments
Any help will be welcome (or any other way to proceed if I'm wrong).
Thank you
If you are able to draw the google chart in your local server, but not in production server, this means it is your server issue.
You can add authentication header in your jquery ajax call to make authenticated ajax request
$.ajax({
headers: {
"Authorization": "Bearer <TOKEN HERE>"
}
})
You can also send username and password in jquery ajax call, to make authenticated request. Here is the sample code from the link
$.ajax({
type: 'GET',
url: 'url',
dataType: 'json',
//whatever you need
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth(user, password));
},
success: function () {});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
at the end, I tried to run the ajax request through dojo instead of Jquery.
My codes became this one :
var jsonData = dojo.xhrGet({
url: "http://server/database/agent?openagent",
handleAs:"json",
...
})
I did no changes at the security level or anything else.
I do not understand why the jquery syntax is not working as well the dojo syntax.
anyway, it is working now.
Many thanks to all for your suggestions

tumblr api v2, get posts return 404 Not found

I just discover the tumblr API (http://www.tumblr.com/docs/en/api/v2)
I have created a new blog (protected with password) and also generated an API_KEY.
Here is my ajax request :
$.ajax({
type: "GET",
url : "http://api.tumblr.com/v2/blog/myblog-dev/posts/photo?api_key=MYAPIKEY",
dataType: "jsonp",
success: function (data) {
console.log(data);
}
});
And the result is :
{"meta":{"status":404,"msg":"Not Found"},"response":[]}
I can't figure out what i'm doing wrong, what am I missing ?!
http://api.tumblr.com/v2/blog/{$blogname}.tumblr.com/posts/photo?api_key={$public_consumer_key}
This works for me, try updating your code from what you have, to this.
Obviously replace {$blogname} with the blog name that has been authenticated with your application that you are trying to access, and obviously replace {$public_consumer_key} with your registered applications' public consumer key.

How to send and retrieve cross-domain ajax data in userscript

I use this code to store and retrieve ajax data via http://openkeyval.org/
$.ajax({ /* send data */
url: "http://api.openkeyval.org/store/",
data: "test-key-data=" + JSON.stringify([123,456]),
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
$.ajax({ /* retrieve data */
url: "http://api.openkeyval.org/test-key-data",
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
everything work fine in Chrome javascript console but in userscript I get error like this
Uncaught ReferenceError: jQuery110208458673823624849_1375932537303 is
not defined
I try to use GM_xmlhttpRequest to retrieve data like this
GM_xmlhttpRequest({
method: "GET",
url: "http://api.openkeyval.org/test-key-data",
onload: function(response) {
console.log(response.responseText);
}
});
but it seem like openkeyval doesn't accept data via POST/GET method and log result was like when you access it directly from url of browser like this
{"error":"not_found","documentation_url":"http://openkeyval.org/"}
I include jQuery and it work fine with this code
// #require http://code.jquery.com/jquery-latest.min.js
I try to use Greasemonkey/jQuery XHR bridge with out change other code by like this
// #require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js
and try use openkeyval official javascript library with code like this
// #require http://cdn.openkeyval.org/statics/openkeyval.packed.js
and retrieve data with code like this
var ourCallback = function(value, key) {
console('The value of ' + key ' + is ' + value);
};
window.remoteStorage.getItem('test-key-data', ourCallback);
still got error ERROR: Unexpected string
Please help, I mess with it more than 10 hours. Thank you so much.
It look like $.ajax always trigger error event function
but GM_xmlhttpRequest can retrieve mistype data, so I try looking for dataType: "jsonp" in GM_xmlhttpRequest and I got that jsonp header content-type is "application/javascript" OR "application/json" and the first one work well.
my new code for retrieve data look like this
GM_xmlhttpRequest({
method: "GET",
url: "http://api.openkeyval.org/test-key-data?nocache=" + new Date(),
headers: {
"Content-Type": "application/javascript"
},
onload: function(response) {
console.log(response.responseText);
}
});
and retrieve data using $.ajax even it always trigger error event function but it still send data.
I try both content-type on GM_xmlhttpRequest and still not work.
my code to store data look like this
$.ajax({ /* send data */
url: "http://api.openkeyval.org/store/",
data: "test-key-data=" + JSON.stringify(myVarObject),
dataType: "jsonp"
});
Add this into $.ajax({...})
crossDomain: true;
It is because by default cross domain ability is disabled. See http://api.jquery.com/jQuery.ajax/
EDIT:
Sometimes there will be a issue with different charset between local script and remote script. Try using:
scriptCharset: "utf-8";
Also look at JQuery AJAX is not sending UTF-8 to my server, only in IE
Elaborating my comment
The reference is to the callback function generated by jquery.
It Sounds to me the way you invoke your userscript unloads the jquery functions before the callback is executed.
Perhaps you use a link and forgot the preventDefault?
If you ajax and have
$("#linkid").on("click"
or
$("#formid").on("submit"
it is MANDATORY to continue like this:
,function(e) {
e.preventDefault();
Otherwise the link is followed or the form is submitted which may not have any visible effect, but the asynchronous scripts have been (partially) unloaded unless the form and link has a target other than the current window

Gravatar VS JQuery Ajax... help?

I want to include a function on my page, that checks whether the user has a Gravatar account with their email. If yes, they should have that picture displayed, if not they should be given other options.
I'm trying to do this as follows:
$.ajax({
url: 'https://secure.gravatar.com/' + md5(user.email) + '.json',
method: 'GET',
timeout: 4000,
success: function successFn() {
doGravatarStuff();
},
error: function errorFn(response, status, error) {
console.log(response.status); //debug
}
});
This always returns an error status of 0 on Internet Explorer and I can't seem to figure out why. I tried changing the 'dataType' to 'json', 'html' etc but that doesn't seem to help.
Also, and maybe that is a related problem, if I test this on FF or Crome, with a user that really doesn't have an account, it returns a 404-error according to the 'net' tab readout
404 Not Found 649ms
but 'response.status' still seems to be 0
Any ideas anyone? Thanks so much in advance!!
Normally cross-domain AJAX requests are denied because it's a security thing. See this blog article: http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
Did you try the jsonp dataType? I use jsonp when I access twitter's tweet json feed.
Example:
var urls = "http://twitter.com/status/user_timeline/" + username + ".json?count=" + pageSize + "&page=" + currentPage;
$.ajax({
beforeSend: function () {
$("#ajax-load").fadeIn()
},
url: urls,
cache: true,
type: 'GET',
dataType: 'jsonp',
success: twitterCallback2
});
};

Resources