Working with jquery ajax in Codeigniter framework.
Sending ajax request as below (onclick of a button)
$.ajax({
type: "POST",
url: "user_registration",
data: {
first_name: first_name,
middle_name: middle_name,
last_name: last_name,
position: position,
company_name: company_name,
address_line1: address_line1,
address_line2: address_line2,
phone_number: phone_number,
fax_number: fax_number,
mobile_number: mobile_number,
email_id: email_id,
company_website: company_website,
username: username,
password: password,
news_letter: news_letter
},
cache: false,
success: function (result) {
$("#please_wait").hide();
$("#registration_results").html(result);
},
error: function (xhr) {
console.log("Error: " + xhr.statusText);
}
});
Possible solutions i found in different forums are as below
CSRF security should be disabled (Changed TRUE to FALSE in config file)
Tried to send the request with dataType:jsonp and also with dataType:json.
As i am using onclick method placed return false at the end of the function.
used security->get_csrf_token_name() ?> : 'security->get_csrf_hash() ?> in data{}.
I tried with all the above ways. But still i am getting 505 / cancel status.
Note : Previously (some couple of days back) this code is working fine, but now it is giving trouble. Ajax requests with the same format in other pages are working fine.
Please suggest me what may be the problem.
Thanks sreeram,
Related
I have a script with autocomplete, get some data from an external source according to searched term.
I can output the json in the console but I'm struggling to pass it to the response, how do I do that?
$('#test').autocomplete({
source: function(request,response){
$.post('/schoollookup', {
query: request.term
}, function(data){
}, 'json'
);
},
minLength: 2
});
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
This is the syntax for post request. where
url : A string containing the URL to which the request is sent.
data : A plain object or string that is sent to the server with the request.
success : callback function
#Sumesh
$.post('/schoollookup', {
should be working the same, the difficulty that I have is to get response
Thank you for your answer r007ed, the issue was that it was not returning an array.
So the final code for this is :
$('#test').autocomplete({
source: function(request,response){
$.post('/schoollookup',{query: request.term}, response, 'json');
},
minLength: 2
});
I have a page in PHP that does a query in the database at the end of the processing I show the amount of updated records. This process takes a while, in my Jquery I just show the total of affected records, how could I change my Jquery to show from time to time, in case every 1 second it shows the user the amount of records.
var form_data = new FormData();
form_data.append('id', idSel);
$.ajax({
url: 'post.php',
type: 'POST',
data: form_data,
cache: false,
contentType: false,
processData: false,
beforeSend: function () {
$('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> ");
},
success: function(response) {
ATTENTION I can not keep calling the PHP page several times, I need to call once and listen to it or receive data from it!
Is it possible for teachers?
It's happening something quite strange to me. I'm trying to use this plugin, at friday I saw working it completelly fine.
But I don't know what happened... the onComplete event is not firing. I had tested several ways to define it, these:
var fineUploader = $('#fineUploader');
fineUploader.fineUploader({
request:{
endpoint: '/wp-content/plugins/innovation-factory/includes/php/endpoint.php'
},
callbakcs:{
onComplete: function(id, name, responseJSON, xhr){
console.log(responseJSON);
debug.log({
_id: id,
_name: name,
_responseJSON: responseJSON,
xhr
});
}
},
onComplete: function(id, name, responseJSON, xhr){
console.log(responseJSON);
debug.log({
_id: id,
_name: name,
_responseJSON: responseJSON,
xhr
});
}
});
fineUploader.on('onComplete', function(id, name, responseJSON, xhr){
console.log(responseJSON);
debug.log({
_id: id,
_name: name,
_responseJSON: responseJSON,
xhr
});
});
I had tested theese 3 ways one by one, separatly.
What I'm doing wrong?
PD: There is no errors in console, and the files are correctly saved...
The issue with your code is only caused by the fact that you spells "callbacks" wrong. If you fix your spelling error, the issue disappears.
The response I'm getting back from PHP using an AJAX call:
$.ajax({
url: 'getit.php',
type: 'POST',
dataType: 'json',
data: {id_token: my_token},
success: function(data) {
//stuff
}
});
gives me something like this when I console.log it:
email: "me#example.co"
email_verified: true
first_name: "Bob"
permissions: Object
client_1001: Object
client_id: "121434"
role: "full"
table_name: "5tyyd"
client_1002: Object
client_id: "45638"
role: "full"
table_name: "df823"
If I do something like this:
$('.last_name').text(data.first_name);
and
<div class="last_name"></div>
This gives me back Bob just as expected.
What I need to get is a list of permissions (e.g., client_1001) and the pieces of data under each permission. I can't figure out how to do though.
This is where I'm stuck:
$('.last_name').text(data.permissions.WHATGOESHERE?);
I need to fill in the WHATGOESHERE? part. It's basically an object in an object, but I'm not sure how to parse it.
As you're using jQuery, give this a try :
$.each(data.permissions, function (index, value) {
console.log(index); // "client_0001"
console.log(value.table_name); // "5tyyd"
});
The idea is to iterate through the response, as if you were using foreach() in PHP.
I am doing this facebook invitation (apprequest) but i need to count the number of invitation made by the user, anybody can help me?
The code below works but it only counts 1 invitation no matter how many invites the user sends
<script>
FB.init({
appId:'<?=$myappid?>',
cookie:true,
status:true,
xfbml:true
});
function FacebookInviteFriends()
{//start of FacebookInviteFriends
var inviteCounter = FB.ui({
method: 'apprequests',
message: '<?=$me['name']?> has invited you to play this game!',
title: 'Select your friends to play this game!',
exclude_ids: <?=$allFBStr?>,
max_recipients: 5
},
function (inviteCounter) {
var userID = '<?=$me['id']?>';
//alert(userID);
$.ajax({//ajax
type: 'POST',
url: 'addInvitationCounter.php',
data: {
userID: userID
},
success: function(result){
//alert("SUCCESS: " + result);
location.href = "myprofile.php";
},
error: function(result){
alert("ERROR: " + result);
}
});
}
);
}
</script>
I don't know if i got your question right, but this is actually against the Facebook policy.
Per section V.1.
You must not incentivize users to grant additional permissions or use
Application Integration Points.
And per the documentation about Application Integration Points
By "Application Integration Point" we mean Application Info Section,
Application tab, Feed, requests (including invites), Publisher, inbox
attachments, Chat, Bookmarks, or any other feature of a user profile
or Facebook communication channel in which or through which an
application can provide, display, or deliver content directed at, on
behalf of, or by permission of a user.
I once wrote an app, where you were getting bonuses depending on how many people you invite, so it is possible. I think you missed to structure of the response object.
When I run the invite code similar to this:
FB.ui({
method: 'apprequests',
message: 'XYZ has invited you to play this game!',
title: 'Select your friends to play this game!'
}, function (response) {
console.log(response);
});
This is what response looks like (IDs are changed, see the XYZ)
{
request: "634814526537XYZ",
to: [
0: "100000526972XYZ"
1: "100000756092XYZ"
]
}
So there's always two entries in the resonse object, one of them is 'to', an array with the invited ids. The number of invited people is accessed like this:
FB.ui({
method: 'apprequests',
message: 'XYZ has invited you to play this game!',
title: 'Select your friends to play this game!'
}, function (response) {
var numberOfInvites = response.to.length;
alert('You have invited '+ numberOfInvites +'People');
});
But I don't know of any policies permitting this, so that's a topic you should read about.