I want to know where should I put my ajax call when the user clicks the send request button on the fb ui invite. Currently, I put it after the fb ui syntax:
function sendRequest(gFriendID, gFriendName) {
FB.ui({
method: 'apprequests',
title: 'Check out LoyaltyNmore!',
to: gFriendID,
message: 'Hi ' + gFriendName + ' join me in earning loyalty points!',
}, fbCallback);
$.ajax({
type: "POST",
url: "/home/StoreInvite?userId=" + gFriendID,
success: function (data) {
},
error: function (err) {
}
})
}
}
But as expected. The ajax call gets executed when the app opens up the request UI. I want the ajax call to be executed right after the user clicks send request not when the request UI shows. Also when cancel is clicked. I don't want my ajax call to be executed. Any ideas? Thanks
You basically need to put your ajax call inside the facebook callback function... The response will contain details about to whom the app was shared. The 'to' array will get you the ids of those who were invited.
function sendRequest(gFriendID, gFriendName) {
FB.ui({
method: 'apprequests',
title: 'Check out LoyaltyNmore!',
to: gFriendID,
message: 'Hi ' + gFriendName + ' join me in earning loyalty points!',
}, function(response)
{
if(response.to.length)
{
$.ajax({
type: "POST",
url: "/home/StoreInvite?userId=" + gFriendID,
success: function (data) {
},
error: function (err) {
}
});
}
});
}
Related
I'm initializing three functions on loading the page and then performing some events on click so I want that when a click event is pressed I just reload those functions completely in the given code it loads the function again but the previous one remains there so I want that when I load the function again it should remove the previously loaded one.
$('#locationdeleteconfirm').click(function(e) {
e.preventDefault();
$.ajax({
url: "php/deleteLocationByID.php",
type: 'POST',
dataType: 'json',
data: {
name: $('#dname').val(),
locationID: $('#dlocation').val(),
id: $('#editlocationid').val(),
},
//if user enters correct data and api gives back the data then we run success funtion
success: function(result) {
console.log(result);
//if status of data is ok we fetch the data from fields and put them in results table in html
if (result.status.name == "ok") {
//fetching fields from api and showing them in html table
Swal.fire({
title: "Deleted",
text: "Deleted",
type: "success"
}).then(function() {
$("#locdeleteconfirm").modal("hide");
$("#editlocationmodal2").modal("hide");
$('.modal-backdrop').removeClass('modal-backdrop');
$('#locationtable').load(location.href + " #locationtable>*")
getlocations();
getdepartments();
getallemployees();
});
} else {
//if the upper condition fails
console.log("error");
}
},
//if there is no success in retrieving data from api
error: function(jqXHR, textStatus, errorThrown) {
console.log("error");
$('#txterror').html("Enter Valid Id");
}
});
})
getloactions();
getdepartments();
getallemployees();
these are the three functions that are loading on the page load event I want them to reload once the click event is performed
I need to send an XML type data to backend using jquery, ajax as a DELETE request. This returns empty array from backend request body. How can I send id properly?
here is my code,
function deleteProduct(id) {
var xmlDocument = $(
`<productsData>
<Prod_ID>${id}</Prod_ID>
</productsData>`);
$.ajax({
type:"DELETE",
url:"http://localhost:8000/delete",
data:JSON.stringify({
data : xmlDocument
}),
contentType: 'application/json',
dataType: 'text'
});
}
I need to send this data,
<productsData>
<Prod_ID>2</Prod_ID>
</productsData>
this 2 comes from the function parameter.
this is my backend in express
app.delete('/delete',(req,res,next)=>{
console.log(req.body);
res.status(200).json({
message: "success"
})
})
this returns empty object.How can I solve this?
If you want to send XML, don't say you're sending application/json:
function deleteProduct(id) {
return $.ajax({
type: "DELETE",
url: "http://localhost:8000/delete",
data: `<productsData><Prod_ID>${id}</Prod_ID></productsData>`,
contentType: 'application/xml'
});
}
By returning the Ajax request, you can do something like this:
deleteProduct(42).done(function () {
// delete completed, remove e.g. table row...
}).fail(function (jqXhr, status, error) {
// delete failed, keep table row & show alert
alert("Could not delete product: " + error);
});
The user of my Cordova/PhoneGap app has to sign in, so I group all the form's data into a Javascript array, then I JSON.strigify() it and I send it to the server via an Ajax request.
The PHP script has been tested by manually sending the CGI command, it does perfectly works.
Also, I used alert() to check if the function sendDataToServer() is called, it is.
The problem is that Ajax doesn't output success() neither error(). AND there is no difference (exept the command, obviously) with an other working script that I use for checking a login.
Note: I use GET for testing purposes, at the end it will be POST.
Here is the WORKING script:
$("#connectionFormSubmit").click(function () {
$.ajax({
type: "POST",
url: "http://" + host + "/my_app/check_login.php",
data: "login=" + $("#login-conn").val() + "&password=" + $("#password-conn").val(),
success: function (msg) {
navigator.notification.alert(msg, alertDismissed, 'Result', 'OK');
},
error: function (jqXHR, exception) {
navigator.notification.alert(getError(jqXHR, exception), alertDismissed, 'Error', 'OK');
}
});
});
Here is the NOT WORKING script:
function sendDataToServer(dataGuest, dataChild1, dataChild2, dataChild3) {
$.ajax({
type: "GET",
url: "http://" + host + "/my_app/insert_new_user.php",
data: "guest=" + dataGuest + "&child1=" + dataChild1 + "&child2=" + dataChild2 + "&child3=" + dataChild3,
/* async: false, not changing anything */
success: function (msg) {
navigator.notification.alert(msg, alertDismissed, 'Result', 'OK');
},
error: function (jqXHR, exception) {
navigator.notification.alert(getError(jqXHR, exception), alertDismissed, 'Error', 'OK');
}
});
}
(in both cases the server script works perfectly fine).
This is how I call the function:
sendDataToServer( JSON.stringify(dataGuest), JSON.stringify(dataChild1), JSON.stringify(dataChild2), JSON.stringify(dataChild3) );
I didn't find anything simiar to my problem on Google.
I am working on sencha touch where if i pass a data to a server.it should brings the response by checking it's database. it's working now. but when i pass the data which is not in the server database it shouldn't bring a response. Loading Mask keeps loading...Here's my code
store.load({
params: {
name: 'xxxxx',
},
url:'search.php',
/*NOT WORKING
success:function()
{
},
failure:function()
{
}*/
});
is there any thing like ajax request call like success/failure method.
When you call the load method from a store, there's a callback property that you have to populate:
callback: function(records, operation, success) {
//the operation object contains all of the details of the load operation
console.log(records);
}
Full text here (applies to both ST1 and ST2):
Sencha Touch API Docs for Ext.data.Store
This is how you can make an Ajax request
Ext.Ajax.request({
url: '140.45.45.20:9010/TService/Sms/Services',
params: form.getValues(),
method: 'GET',
success: function(response, opts) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
//The request was successful - see the response in the response object
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response.status);
}});
How can I get the names of the Facebook friends of the user?
I send requests to using FB.ui({ method: 'apprequests'?
Here is my code:
<input id="btSelectFriends" type="button" value="Select Friends" onclick="javascript:FBUISelectFriends()" />
<div id="fb-root"></div>
<script type="text/javascript" src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
function FBUISelectFriends()
{
var selectedFriendsRequest_ids;
FB.ui({ method: 'apprequests', message: 'Select friends', data: 'tracking information for the user', tite: 'Select friends' },
function (response) {
if (!response || !response.request_ids) { alert('Request could not be sent'); return; }
selectedFriendsRequest_ids = response.request_ids;
for (var i = 0; i < selectedFriendsRequest_ids.length; i++) {
FB.api('/me/apprequests/?request_ids=' + selectedFriendsRequest_ids[i].toString(),
function (response) {
if (!response || response.error) { alert('Friends Selection error occured'); return; }
}
)
FB.api(selectedFriendsRequest_ids[0],
function (response)
{
alert(response.name);
}
);
}
}
);
return;
}
</script>
I tried this code but it didn't work:
FB.api(selectedFriendsRequest_ids[0], function (response) {console.log(response.name);});
Can you please help?
Thanks.
Just change this in your function
function FBUISelectFriends()
{
FB.ui(
{
method: 'apprequests',
redirect_uri: 'YOUR APP URL',
message: "Tom sent you a request"
},
function(response) {
if(response && response.hasOwnProperty('to')) {
for(i = 0; i < response.to.length; i++) {
//alert( response.to[i]);
// response.to[i] gives the selected facebook friends ID
// To get name of selected friends call this function below
getfbnames(response.to[i]);
}
}
}
);
}
function getfbnames(selectedfrndid)
{
var url = 'getfriendfbname.php'; // call a php file through URL and jquery ajax
$.ajax({
type:'POST',
url: url,
data : { fbid : selectedfrndid },
async: false,
success: function(data)
{
alert(data);
}
}); // end of ajax
}
A file getfriendfbname.php which returns the name of facebook friend name using friend facebook id in php
$fbid=$_POST['fbid'];
$json = file_get_contents('https://graph.facebook.com/'.$fbid);
$data = json_decode($json);
echo $data->name;
return $data->name;
Since 30 Sept. Facebook released a Requests 2.0 implementation of their system, which is a LOT more useful than the old one.
Here's why :
With the old one, your user first sent requests, Facebook returned to you the request_ids, then you had to verify who you sent the requests to...
With the new one, for each group of requests (like when a user sends multiple requests using multi-friend-selector) Facebook returns you one request id (parameter : response.request) and a parameter with all Facebook user ids requests were sent to (parameter : response.to)
You just have to activate Requests 2.0 in your app dashboard, then just change the conditions in your Javascript callback.
There's requests to retrieve information about your user's friends FB.api('/me/friends?fields=id,name,first_name,last_name and you just have to parse the result of this request with the Facebook user ids you sent requests to !
Everything is explained here about Requests 2.0 : http://developers.facebook.com/blog/post/569/