0

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'];
}
go_live
  • 29
  • 7
  • 2
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Oct 22 '21 at 13:10
  • $headers undefined ?! Return ? – bZezzz Oct 22 '21 at 13:13
  • A 500 error is a general error, but errorThrown should return the error message, I think the reason why errorThrown could be empty is because the request was made with HTTP/2. This is because the property comes from the statustext property of the underlying XHR, and that property does not get populated for HTTP/2 requests. for more info here: https://stackoverflow.com/questions/41632077/why-is-the-statustext-of-my-xhr-empty – Abdlrahman Saber Oct 22 '21 at 13:26
  • @bZezzz When I commented out $ header and $ return, the error still appeared, so it doesn't seem to be the effect of them. – go_live Oct 23 '21 at 05:04
  • @AbdlrahmanSaber I see, how should I solve it? – go_live Oct 23 '21 at 05:06

0 Answers0