WORDPRESS : Ajax and template
I have a question.
How can I use Ajax on my templates...
in single.php I have :
$.ajax({ type: "POST", url: "http://www._____wp-content/themes/MS-MangoBerry___/myajax.php", data: "yo", cache: false, success: function(data) { alert("yes"); } });
A开发者_开发知识库nd in myajax.php, I have
$(document).ready(function() { alert("ok"); });
Then I have an error : Fatal error: Call to undefined function get_header() in myajax.php
Why ?
Thanks in advance.
Please also have a look at this article http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global
It suggests that all AJAX requests should be submitted to /wp-admin/admin-ajax.php
And the you could hook the request by using this code in functions.php
add_action('wp_ajax_your_ajax_action_name', 'method_name'); add_action('wp_ajax_nopriv_your_ajax_action_name', 'method_name');
Then you could implement a method in functions.php
function method_name() { // do something or echo XML to return stuff }
On the request you also need to send a parameter name 'action' with value of the action name.
in this case it would be action=your_ajax_action_name.
Hope this help :)
wordpress has a built ajax url that you need to use. this post will help you out. http://geekpreneur.blogspot.com/2009/06/how-to-use-wpajax-in-wordpress.html
the tricky thing is how wordpress knows which function will accept your call back. it happens by adding an action. the hook of the action is your ajax action prepended with wp_ajax_
精彩评论