0

I’m working on a WordPress plugin and I need to make a query to the DB using AJAX.

My function looks like this:

function kokoku() {
    if ($(".kokoku_head").height() === 0) {
       $.ajax({
           url: 'location_query.php', 
           success: function(data) {
               $(".kokoku_head_bup").html(data);
           }
        });
     }
};

Now in my location_query.php if I go for something static, like:

Echo “hey!”;
Return

Everything goes well. However the minute I add WP_Query() I get a No 'Access-Control-Allow-Origin' header is present on the requested resource. Error.

I have tried adding

header('Access-Control-Allow-Origin: *');

to the location_query.php file but the problem still persists.

My best guess is WP_Query() is calling yet another file which is is returning the No 'Access-Control-Allow-Origin' error and were I should add the header('Access-Control-Allow-Origin: *'); too, but I don’t know which file that might be.

Guillermo Carone
  • 818
  • 2
  • 9
  • 24
  • 2
    AJAX in WordPress doesn't work that way. https://codex.wordpress.org/AJAX_in_Plugins – Nathan Dawson May 01 '17 at 15:36
  • 1
    Yes, totally wrong approach... This answer of mine has a fully working example, should be easy to adapt to your needs: [How to Use AJAX in a WordPress Shortcode?](http://stackoverflow.com/q/13498959/1287812) – brasofilo May 04 '17 at 03:41

0 Answers0