I
I
IgorPlays2021-12-29 08:05:14
AJAX
IgorPlays, 2021-12-29 08:05:14

Can't give an AJAX request?

I wanted to give a request for one API with Basic Auth (USERNAME - PASSWORD).
Created index.php - added header header('Access-Control-Allow-Origin: *');
Wrote script - send AJAX on button click

function myFunction(){
        var username="xxx";
        var password="xxx";
        $.ajax({
          type: "GET",
          url: "xxx",
          dataType: 'json',
          headers: {
            "Authorization": "Basic " + btoa(username + ":" + password)
          },
          success: function (result){
              console.log(result)
          }
        });


In response I receive
Access to XMLHttpRequest at 'xxx' from origin 'https://Мой серв.kz' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Tried a lot of things but it doesn't help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-12-29
@Rsa97

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question