I want to get the url sent by Ajax in fuctions.php on the site made by Wordpress.
With the current code, the following error is displayed on the console.
I checked it from the network with the verification tool, but the preview is a white background and no response is displayed.
I would like to find out the cause of the error, what should I do?
Error
XMLHttpRequest : 500
textStatus : error
errorThrown : undefined
Code
main.js
(function($) {
var url_category = location.href;
var ajaxUrl = window.location.protocol + '//' + window.location.host + '/wp-content/themes/example/functions.php';
$.ajax({
url: ajaxUrl,
type: "GET",
data: { url_category: url_category },
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("Failed!");
console.log("XMLHttpRequest : " + XMLHttpRequest.status);
console.log("textStatus : " + textStatus);
console.log("errorThrown : " + errorThrown.message);
},
success: function (response) {
console.log("Success!");
console.log(url_category);
}
});
})(jQuery);
functions.php
add_action('wp_ajax_get_case', 'dk_get_case');
add_action('wp_ajax_nopriv_get_case', 'dk_get_case');
function dk_get_case() {
$headers['Access-Control-Allow-Origin'] = '*';
$return = ['status' => false, 'data' => [], 'message' => ''];
$url = $_POST['url_category'];
}