Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. function getFruits() {
  2. return json_encode( ['fruits' => ['apples', 'pears']]);
  3. }
  4.  
  5. function my_enqueue() {
  6. wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
  7.  
  8. wp_localize_script( 'ajax-script', 'my_ajax_object',
  9. array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
  10. }
  11. add_action( 'wp_enqueue_scripts', 'my_enqueue' );
  12.  
  13. $(document).ready(function() {
  14. $.ajax(
  15. {
  16. type: "get",
  17. data : {
  18. action: 'getFruits'
  19. },
  20. dataType: "json",
  21. url: my_ajax_object.ajax_url,
  22. success: function(msg){
  23. console.log(msg); //output fruits to console
  24. }
  25. });
  26. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement