Here's my ajax call:
$("#yes, #no").click(function(){
var upOrDown = $(this).attr('id');
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
action: "updateVote",
data: {upOrDown: upOrDown},
dataType: "json",
success: function(data) {
console.log(data.output);
}
});
return false;
});
Here's the function in functions.php
function updateVote() {
echo json_encode(array(
'output' => 'hello!'
));
die();
}
add_action('wp_ajax_updateVote', 'updateVote');
add_action('wp_ajax_nopriv_updateVote', 'updateVote');
Why does this keep returning "0". It should return "hello!"
Thanks.
Related
I am new to wordpress, i make a ajax request on click button and it print the data, but ajax is not giving me any response. Please help me to find out the error.
Here is my code
add_action("wp_ajax_delivery_options", "delivery_options");
add_action("wp_ajax_nopriv_delivery_options", "delivery_options");
function delivery_options()
{
echo json_encode(array('type' => 'success'));
wp_die();
}
wp_enqueue_script("my-ajax-handle", get_stylesheet_directory_uri() . "/js/custom.js", array('jquery'));
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' => admin_url('admin-ajax.php')));
Ajax
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});
})(jQuery);
Any solution appreciated!
you have to pass "callback function name" in data: { action: 'delivery_options', delivery_option: data },
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { action: 'delivery_options', delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});})(jQuery);
I have a GirdView which includes checkbox. Now I have a button which i routed to another action controller. Below is my code
<?= GridView::widget([
'dataProvider' => $dataProvider,
/*'filterModel' => $searchModel,*/
'id'=>'grid',
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
'Meter Serial Number',
'Issued To',
'Store',
],
]); ?>
Set PDF
$(document).ready(function () {
$('#myid').click(function() {
var keys = $('#grid').yiiGridView('getSelectedRows');
// alert(keys);
$.post({
url: 'ogpheader/viewsetpdf',
dataType: 'json',
data:{keylist: keys},
success:function(data) {
alert('Done')
}
});
}) });
Then in my controller
public function actionViewsetpdf()
{
/*$model = $this->findModel($id);
print_r($model);*/
if(isset($_POST['keylist']))
{
$keys = json_decode($_POST['keylist']);
print_r($keys);
}
exit();
}
When I click on the button i get empty view. I followed this tutorialI don't know what is the problem. I am stuck to it.
Update1
While checking it in network
Update 2
As suggested I have tried with $.ajax and below is the result
Update 3
After changing the JS
$('#myid').click(function(e) {
e.preventDefault();
var keys = $('#grid').yiiGridView('getSelectedRows');
// alert(keys);
$.ajax({
url: '<?= URL::toRoute(["ogpheader/viewsetpdf"])?>',
dataType: 'json',
data:{keylist: keys},
type: 'post',
success:function(data) {
alert('Done')
}
});
The result is
Any help would be highly appreciated.
Change the controller and see what return, probably csrf token missing that why you got empty output
public function actionViewsetpdf()
{
if(isset($_POST['keylist']))
{
$keys = json_decode($_POST['keylist']);
print_r($keys);
}
else{
echo 'no data';
}
exit();
}
POST method required csrf token so you have to pass _csrf token as a parameter
$.ajax({
url: 'ogpheader/viewsetpdf',
type: 'post',
dataType: 'json',
data: {
keylist: keys,
_csrf: '<?=Yii::$app->request->getCsrfToken()?>'
},
success: function(data) {
console.log(data);
}
});
Or you can disable csrf valdation by adding this to your controller
public function beforeAction()
{
if ($this->action->id == 'viewsetpdf') {
Yii::$app->controller->enableCsrfValidation = false;
}
return true;
}
Or simplest way just change POST to GET
$.post() has limited parameter to pass. Use $.ajax(). Also you need to add e.preventDefault() to stop redirection from a tag
$(document).ready(function () {
$('#myid').click(function(e) {
e.preventDefault();
var keys = $('#grid').yiiGridView('getSelectedRows');
// alert(keys);
$.ajax({
url: '<?php echo URL::toRoute(["ogpheader/viewsetpdf"]); ?>',
dataType: 'json',
data:{keylist: keys},
type: 'post',
success:function(data) {
alert('Done')
}
});
}) });
Use below js
<?php
$url = Url::toRoute(['ogpheader/viewsetpdf']);
$this->registerJs(<<< JS
$(document).ready(function () {
$('#myid').click(function() {
var keys = $('#grid').yiiGridView('getSelectedRows');
$.ajax({
url: '$url',
data: {keylist: keys},
type: "POST",
dataType: 'json',
success:function(data) {
alert('Done');
}
});
});
});
JS
); ?>
I have this ajax function:
$(document).ready(function(){
setInterval(function() {
$.ajax({
url: 'php.php',
type: 'POST',
success: function(data){
if( data != "0" ) {
alert(data.a);
}
},
});
}, 5000);
});
and a PHP to return:
<?php
header('Content-type: application/json');
...some function
if($num>0){
echo json_encode(array("a" => "valueA", "b" => "valueB"));
}
else{
echo json_encode(0);
}
?>
when I have the alert message, it shows me "UNDEFINED" instead of "valueA".
Is something wrong with my array or ajax?
Set dataType: 'json' to your $.ajax call:
$.ajax({
url: 'php.php',
type: 'POST',
dataType: 'json',
success: ...
dataType allows you to define type of data returned from server. By default, there's intelligent quess and maybe that's not enough.
Hello all I would like to send a request after my first request succeeds
The following is what I am using to make the initial request
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
}
});
return false;
});
In order to make the second requewst I am using the following that is not working, it is loading the page but it is loading the page which I don't want. I am sure I am doing this wrong but any help getting me past this would be great.
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
}
});
return false;
});
Any reason why do you get twice checkUserExists ?
$('#registerUser').submit(function (e) {
e.preventDefault();
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize(),
success: function () {
// IF YOU WANT TO CALL A SECOND AJAX REQUEST
$.ajax({
type: 'get',
url: 'GM/checkUserExists',
data: $('form').serialize()
// success: function ....
});
}
});
// return false; // DONT NEED IT IF YOU HAVE e.preventDefault();
});
I am trying to get ajax working with a codeigniter installation.
This is my PHP function:
function test() {
return print_r("hey");
}
This is the JS:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
success: function(data) {
alert(data);
}
});
This works perfectly but, as soon as I add data it doesn't work.
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: {bar:"foo"},
success: function(data) {
alert(data);
}
});
Thanks in advance!
please check the following
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "&bar=foo&isAjax="+true,
success: function(data) {
alert(data);
}
});
And controller...
function test() {
if($this->input->post('isAjax')){
return print_r("hey");
}
else{
//do another thing
}
}
And another thing if you want to add data in json format then you have to add another property in your $.ajax object that is datatype: "json"
Use following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
dataType: 'json',
data: {'bar':'foo'},
success: function(data) {
alert(data);
}
});
Or you can use shorthand version:
$.post("http://localhost/code/test", {'bar':'foo'}, function(data) {
alert(data);
});
And your php code should be:
function test() {
echo "hey";
}
Try the following:
$.ajax({
type: "POST",
url: "http://localhost/code/test",
data: "bar=foo&name=cyberbob",
success: function(data) {
alert(data);
}
});