Facebook count using ajax - javascript

I have a problem with getting number of shares of my custom url. Im trying this code:
$.ajax({
type: "POST",
url: "http://graph.facebook.com/https://www.myurl.com/somelink/?callback=?",
processData: true,
contentType: 'application/json',
success: function(r){
var fbjson = JSON.stringify(r);
console.log(fbjson);
}
});
I've tried also:
$.getJSON('http://graph.facebook.com/https://www.myurl.com/somelink/?cal‌​lback=?'), function (data) {
fbjson = JSON.stringify(fbjson);
console.log(fbjson);
};
When I paste url into browser I recieving JSON with all needed information, but when I am using $.ajax etc. I am recieving totaly different json or some error that I should use XMLHttpRequest or some other serwer status information. Important knowladge may be that I am using https://.
Anyone know how to correctly get FB share count for custom URL ?
PS Be understanding for me, this are my first steps in JS, thanks :)

For the json might be cooke issue. your browser have cookie for that URL but in ajax you need to pass that cookie !
Why you are counting share count manually ?
There is lots of share tools is available for fee !
example : http://js-socials.com/
I think this plugin will give you what you need !
Thanks

I've solve it. This script work correctly ;)
(function fbcount() {
var token = 'token here',
url = 'url here';
$.ajax({
url: 'https://graph.facebook.com/v2.7/',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, id: url},
success: function(data){
$('.facebook-scount').append(data.share.share_count);
},
error: function(data){
}
});
})();
Thanks!

Related

Ajax not working with a specific file name

Something really weird happened today and I cannot understand its behavior so if someone can help me to understand I would really appreciate it,
So I tried to do ajax call like below
var dto = {
'entity': 'SOME_VAL'
};
$.ajax({
type: 'GET',
url: '/WebService/checkout/applyCoupon.php',
dataType: "json",
data: dto,
contentType: "application/json; charset=utf-8",
cache: false,
success: function (response) {
console.log('Success!');
},
error: function() {
console.log('Error!');
}
});
and it returned my website's HTML code instead of JSON return values.
After a while I was able to resolve the issue by using the same code with diffrent file names.
url: '/WebService/checkout/applyCoupon.php', //NOT WORKING
url: '/WebService/checkout/applyCoupon2.php', //NOT WORKING
url: '/WebService/checkout/coupon.php', //NOT WORKING
url: '/WebService/checkout/apply.php', //WORKING
url: '/WebService/checkout/ap.php', //WORKING
It seems like it does not work when the file name contains coupon in it.
I have other websites running just fine using the exact same code with the file name applyCoupon.php so I do not understand why it won't work on a specific website. I can just simply use a different file name but I want to understand how/why it could happen. The environment is IIS. Can someone please help me to understand this issue? Thank you!

Node.js endpoint ampersand is getting appended when making an ajax call

I've the below code which I'm using to hit a node.js endpoint. However when it is getting hit, the endpoint URL appends an & to it like this,
http://localhost:3004/expenses/?q=&12/02/2014
Hence I'm not getting the desired result back.
Here is how my code looks like,
$('#myForm').on('submit', (e)=>{
e.preventDefault();
$.ajax({
type: 'GET',
url: 'http://localhost:3004/expenses/?q=',
processData: false,
data: $('#startDate').val(),
contentType: 'application/json',
success:(data, status)=>{
// alert(status);
},
error:()=>{
alert('problem');
}
})
})
Can someone shed some light?
The issue is most likely related to the processData: false telling jQuery to not format the data for the request, and the GET url already containing ? in it. Given that you are not giving the request json, I would suggest reducing your call to simplify the issue.
$.ajax({
type: 'GET',
url: 'http://localhost:3004/expenses/',
data: { q: $('#startDate').val() },
success:(data, status)=>{
// alert(status);
},
error:()=>{
alert('problem');
}
});
If you do not give the processData in the options, it will convert the data you give it to a query param for the request. Given that this is a GET request, it will generate the ?q=<value> for you. And as mentioned in the comments, you do not need contentType: application/json on the options as that is telling jQuery to put the content type on the request so the server knows you are sending it json in the body. Which you are not, :)

Javascript Cross domain URL request

Is it possible to send the cross domain URL request and read the response using JSONP?
Could you please give me some samples?
I am trying to send URL request to a different domain using xhr but couldn't read the response
Please help
Thanks in Advance
You can check with blow example:
$(document).ready(function(){
$.ajax({ // ajax call starts
//crossOrigin: true,
type: "GET",
url: 'http://www.google.com', // JQuery loads areas
dataType: 'json', // Choosing a JSON datatype
async: false,
success: function(data) // Variable data contains the data we get from serverside
{
console.log(data);
}
});
});

Unable to perform a Cross domain ajax call. - JQuery

I have checked many post and tried every logic that were mentioned in various blogs and posts. But I am unable to perform a cross domain ajax call to an IIS server. Please anybody advice what else I should look into or configure it to get working. All your help is greatly appreciated.
Here is my ajax call:
var url = "http://mydomain .com/myauthorizeservice";
var jsonParam = JSON.stringify({ username: 'user007', password: 'pass007' });
$.ajax({
type: "POST",
url: url,
crossDomain: true,
data: jsonParam,
success: fnSuccess,
error: fnError,
dataType: "json",
contentType: "application/json"
});
function fnSuccess() {
alert("Success");
}
function fnError() {
alert("Error");
}
My config in the root web.config:-
Error:-
Access Denied.
I really struggled a lot to make this thing work. Here some points that also matters and restrict the cross domain calls-
Note: I am using WCF REST service. and configurations are for IIS 7.5.
1: Make sure your OPTIONSVerbHandler looks like-
2: Make sure you have the correct ordering in HandlerMappings-
Rest settings and the way to perform ajax call is mentioned in the question.
Happy coding!

jQuery Ajax PUT not firing

The following ajax works exactly as advertised in Chrome. HTTP PUT is used to trigger the insertion of an object into a RESTful API.
$.ajax({
type: "PUT",
url: "/ajax/rest/team/create/",
dataType: "json",
processData: false,
data: JSON.stringify(teamObject),
success: function (response) {
teamObject = response.object;
}
});
I note that the jQuery API docs helpfully tell me that PUT and DELETE may work but are not guaranteed in all browsers. Such as is my problem.
How is a RESTful API supposed to be implemented on the client side with a problem like this?
EDIT: Firebug tells me that FF is indeed issuing a PUT, but for some currently unknown reason it's dying before getting to the server. To repeat, this works fine in Chrome.
EDIT: Fiddler doesn't see the FF attempt at all. :(
I got the following to work.
var payload = JSON.stringify(teamObject)
syncHTTP('/ajax/rest/team/create/', 'PUT', payload);
function syncHTTP(url,method,payload) {
var client = new XMLHttpRequest();
client.open(method, url, false);
client.setRequestHeader("Content-Type", "text/plain");
client.send(payload);
}
I'd rather use jQuery than roll my own tho. :| If anyone ever figures it out, just add an answer and if it works I'll accept it.
Thanks.
$.ajax({
type: "PUT",
url: "/ajax/rest/team/create/",
dataType: "json",
contentType: "application/json",
processData: false,
data: JSON.stringify(teamObject),
success: function (response) {
teamObject = response.object;
}
});
You need to add contentType. When contentType is set to application/json jquery do not try to create JSON object from JSON string but send it as is - as string.

Categories