WORDPRESS : Ajax and template

I have a question.

How can I use Ajax on my templates…

Read More

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");
    }
  });

And 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.

Related posts

Leave a Reply

2 comments

  1. 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 🙂