How can I call a php file that is on the same folder of a js file without explicitly declaring its url?

admin

Administrator
Staff member
I have a php file that contains my api called cityguide-api.php and a javascript file called functions.js which are on the same folder (js).

I want to call an api function from my javascript folder, so I wrote:

php:

Code:
<?php

    function getCities(){
        $cities = get_categories();
        return json_encode($cities);
    }

    if(function_exists($_GET['apifunction'])) {
        $_GET['apifunction']();
    }

?>

and on javascript file I am trying to call it like that:

Code:
jQuery("#city").focus(function(){
        jQuery.getJSON( "cityguide-api.php?apifunction=getCities", function( data ) {
            console.log(data);
        });
    });

But when I try to execute that I am getting a non-found error on the console:

Code:
GET http://localhost/wordpress/wp-admin/cityguide-api.php?apifunction=getCities 404 (Not Found)

How can I call a php file that is on the same folder of my js file without explicitly declaring the url?