I need some help about ajax in wordpress always return 0 - ajax

This is the first time I try ajax in wordpress. I need some help.
in my theme's functions.php, I have:
function returnRandomPosts(){
echo '123';
die();
}
add_action('wp_ajax_returnRandomPosts', 'returnRandomPosts');
add_action('wp_ajax_nopriv_returnRandomPosts', 'returnRandomPosts');
and in my single.php file, I have
<script type="text/javascript">
jQuery(document).ready(function($){
$.post("<?php echo admin_url("admin-ajax.php"); ?>", {"action": "returnRandomPosts"}, function(response) {
alert('response: ' + response);
});
});
</script>
When I run the web page, it always alert "response: 0",
I hope you can give me some hints or tips, thank you.
update:
I checked the has_filter function,
the new wp_ajax_XXXXX has successfully add into $wp-filters,
BUT when I call AJAX into wp-admin/admin-ajax.php, has_filter function return false with wp_ajax_XXXXX
Anyone can give me some hint?

Finally, I solved the problem.
I have a plugin, the function is, to preview another theme by adding "theme=name_of_theme" argument to url.
( For example, preview my homepage with "xxx.xx.xx/?theme=name_of_theme" )
The ajax failed in preview.
While I add the same ajax function code to the online page (ie. activated theme), it successes.
I hope my experience may help somebody.

Related

Magento Autmatically redirect to a template file from another template file

I have a custom template file. There i am displaying a message. So after 10 second from the page load, i need to redirect to the admin login page automatically. I tried $this->_redirect('');. But that did not work.
Can anyone help me please. Thank You.
<script type="text/javascript">
setTimeout(function(){
window.location='<?php echo Mage::getBaseUrl().(string)Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName')?>';
}, 10000);
</script>
Put this script in your file it will works for you
The snippet below will resolve your question. If your site uses jquery, you may just paste this in your template. Otherwise, you need to include jquery in your Magento header.
<script>
jQuery(document).ready(function(){
setTimeout(function() { document.location = '<?php echo Mage::getUrl('adminhtml'); ?>'; }, 10000);
});
</script>

Wordpress FrontEnd Ajax return 0 using Theme

Hi I know this has been asked a few times but I have been through every answer and tried them all with no luck.
I am trying to use Ajax in a Wordpress front end page, I have security in the page to ensure the user is logged in before they can view this page too.
No matter what code i enter my ajax call always returns 0.
function ajaxfoodlookup()
{
echo "ajax fired";
die();
}
add_action('wp_ajax_nopriv_ajaxfoodlookup','ajaxfoodlookup');
add_action('wp_ajax_ajaxfoodlookup','ajaxfoodlookup');
This is what I have in my functions.php (i have also tried exit(); and die($results) where $results = 'ajax fired'; nothing seems to work.
This is what I have in the page to call the ajax;
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
data: { action:'ajaxfoodlookup'},
success: function (data) { alert(data);}
});
The only thing that I have different to the other questions/answers on here is that I am using a bought Theme which does have some code added, is that theme able to hijack my ajax calls? I thought wordpress would execute the ajax call depending on the 'action' in the data supplied?
Please help its driving me crazy?
Thanks
Official WordPress recommendation is to use the ajaxurl variable instead of hard coded one. Insert this before your JS code and change the url of your jQuery.ajax to use ajaxurl.
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
You can also use wp_localize_script to define the ajaxurl

Page Jumps After Ajax content is loaded

I am trying load content via ajax when an <a> is clicked. The code I am using:
<script type="text/javascript">
jQuery(document).ready(function(){
// ajax pagination
jQuery('.znn_paginate a').live('click', function(){
var link = jQuery(this).attr('href');
jQuery('.lay1').html('<div class="zn_ajaxwrap"><div class="zn_ajax"></div></div>');
jQuery('.lay1').fadeOut("slow").load(link+' .post').fadeIn('slow');
});
}); // end ready function
</script>
The problem is When the content is loaded the page jumps to the top. I treid to prevent it with: e.preventDefault(); But then the the ajax loading stopped. I guess it stopped prevented the ajax loading too..
Is there any fix for this?
Thanks
P.S: I am using it on wordpress. Here is the tutorial I followed: http://seonix.org/wordpress-seo/easy-ajax-pagination/
EDIT
There was something wrong with the code. I am now using this without any problem: http://pastebin.com/vbXqmTHq
two things:
your function() should return false.
also the link itself should have href="javascript:void(0);

Ajax reloading page

I am not too familiar with ajax, if you could help me that would be great.
The following script sends the data to my controller in Codeigniter and refreshes the page, however I was wondering if there was a way I could do this without the page totally reloading...hope that makes sense.
'onComplete' : function (event, queueID, fileObj, response, data) {
$.post('<?=site_url('admin/news_manager/uploadify');?>',{filearray: response},function(info){
location.reload();
});
Omit the location.reload(); from your script.
But if you want some part of the page to show data from the post, you need to insert that somewhere in the page.
In other words:
$("#IdOfTheHtmlElement').html(info); instead of location.reload();
I hope this helps.

Ajax append load

it must be jquery
I have file text.html with 6 div in (a,b,c,d,e,f)
In another file i have a div, i like it to populate the content of a+b+c+d+e+f into that single div
I have try .load = but b remplace a
i have try append, but i need a temp var
so now i am stuck
That code get the content from the file textes.html ... div #a and put the content into div #right, but the second libe REMPLACE the content of right a with right b
I like to append the content a + b NOT a over b
$(document).ready(function(){
var temp = load('textes.html #nicolas');
$('#right').append(temp);
var temp = load('textes.html #antoine');
$('#right').append(temp);
.
.
.
.
return false;
});
that code is the idea behind what should work, but i cannot make a ajax .load() to load content into a variable to append the content to the div...
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "textes.html",
cache: false,
success: function(html){
$("#right").append(html);
}
});
});
</script>
That code load the WHOLE html file, i like to get only some selected DIV #
$(document).ready(function(){
$.get("textes.html",function(data){
$("#right").append($("#nicolas",data)).end().append($("#antoine",data));
},'html');
});
I had a similar issue just now and I think I figured out a way to do what we want using the .load() function. It's not pretty but never mind ;)
First off, I added a "TempDiv" to my html with a "visibility:hidden" style.
<div id="TempDiv" style="visibility:hidden"></div>
Then you run the jQuery :
$(document).ready(function(){
$('#TempDiv').load('textes.html #nicolas', function(){
$('#right').append($('#TempDiv').html());
});
});
I'm not sure it's the best way !
PS : That is my first stackoverflow post ;)
try,
$.get('url.php', function(data) {
$("#right").append(data);
});
This sounds like jQuery? Please state what framework you are using since I can't really see any mention of it. Anyway, append should work. Just do something like:
mydiv.append(a.text());
mydiv.append(b.text());
mydiv.append(c.text());
mydiv.append(d.text());
mydiv.append(e.text());
mydiv.append(f.text());
They should all be appended into mydiv.
NOTE: if you also want the html, use the .html() function instead of .text().
JQuery ( http://jquery.com/ ) is a good javascript library that you can use to do an AJAX request to get the other file. See this question for more: Use jQuery to replace XMLHttpRequest

Resources