Ajax request to an API

Asked

Viewed 1,047 times

0

I am trying to request a third party API through the following code:

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script>

    $(function(){
      $.post({
        url: url,
        contentType: "application/json;",
        data:"1",
        headers: {'Authorization': 'Basic token'},
        success: function(data) {
          console.log(data)
        }, error: function(err) {
          console.log('error')
        }
      });
    });

</script>

But I am returning the following message.

Mixed Content: The page at 'link-host' was Loaded over HTTPS, but requested an insecure Xmlhttprequest endpoint 'link-api'. This request has been blocked; the content must be served over HTTPS.

How to solve this problem? Through PHP I managed to make the request.

  • If your page contains SSL, then the request link must be SSL (https). In the PHP this is can be ignored, so it works. There is a gambiarra that you can do: Just create a file api.php and use the cURL to send the data to (third-party) API, receive the content, treat and display to the user.

  • I have to do only with JS =/

1 answer

1

Pedro, can you replace your JS request http with https? As Ricardo said, it is necessary that your request is also made in SSL, otherwise the browser will block the mixed content (HTTP/HTTPS).

When you are making your request for PHP your file is certainly operating without SSL and so there is no blocking, since the content has already been returned by API.

Try making the request with https:// in its variable url.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.