admin管理员组

文章数量:1122832

I have an issue with CORS policy. The request to retrieve URL is being blocked. I tried everything (Access-Control-Allow-Origin: * in .htaccess, theme's function.php, theme's header and wp-config.php file as well) and nothing seems to work. It works perfectly on localhost though.

Here's my JS AJAX script:

$.ajax({
        type: "POST",
        url: Theme_Variables.ajax_url,
        data: ({
            action: 'myaction',
            postID: postID, 
            price: price,
        }),
        success: function(response) {
            response = response.slice(0, -1);
            console.log(response);
            $('.Link').prop('href', response);
            //window.location = response;
        },
        error: function(errorThrown){
            console.log( 'error' + JSON.stringify(errorThrown));
            
        }
    });

The error I get is: Access to XMLHttpRequest at '' (redirected from '.php') from origin '' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin.

Can anyone help? It would'nt be strange if it didn't work in localhost, but it does...

I have an issue with CORS policy. The request to retrieve URL is being blocked. I tried everything (Access-Control-Allow-Origin: * in .htaccess, theme's function.php, theme's header and wp-config.php file as well) and nothing seems to work. It works perfectly on localhost though.

Here's my JS AJAX script:

$.ajax({
        type: "POST",
        url: Theme_Variables.ajax_url,
        data: ({
            action: 'myaction',
            postID: postID, 
            price: price,
        }),
        success: function(response) {
            response = response.slice(0, -1);
            console.log(response);
            $('.Link').prop('href', response);
            //window.location = response;
        },
        error: function(errorThrown){
            console.log( 'error' + JSON.stringify(errorThrown));
            
        }
    });

The error I get is: Access to XMLHttpRequest at 'https://someotherurl.com' (redirected from 'https://myurl.com/wp-admin/admin-ajax.php') from origin 'https://myurl.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin.

Can anyone help? It would'nt be strange if it didn't work in localhost, but it does...

Share Improve this question edited May 29, 2024 at 21:13 Tony Djukic 2,2594 gold badges18 silver badges33 bronze badges asked May 17, 2024 at 12:25 Sam1501Sam1501 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The problem was that the PHP AJAX function was redirecting to another page, which caused these issues. I decided to just generate a link instead of redirecting.

本文标签: AJAX request blocked by CORS policy