How to access the response of third party website when the content-type is text/html ? using java script

87 Views Asked by At

I was trying to get the response for my get request to this website https://pubmed.ncbi.nlm.nih.gov/?term=hello I am getting error :

Request failed with status code 200

warning :

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://pubmed.ncbi.nlm.nih.gov/?callback=jQuery360008810428095163192_1684733477188&_=1684733477189 with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

code :

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script>
    function makeCorsRequest() {
    const apiUrl = 'https://pubmed.ncbi.nlm.nih.gov/'; // URL of the API you want to access

    $.ajax({
        url: apiUrl,
        type: 'GET',
        dataType: "jsonp",
        contentType: "application/json",
        crossDomain: true,
        success: function(response) {
        console.log(response);
        },
        headers:{
            'Access-Control-Allow-Origin': '*',
        },
        error: function(xhr, status, error) {
        console.error('Request failed with status code ' + xhr.status);
        }
    });
    }

    // Make the CORS request
    makeCorsRequest();

</script>

I can access but the content is blocked by CORB. Content type given in this website is text/html; charset=utf-8. how to make change to this code

I tried to change the content-type did not work I noticed that someone changing the website content to xml to json and getting that as response. but i am not sure that the webiste i am working with have such a json format or jsoncallback

1

There are 1 best solutions below

0
Yum Lee On

This error has nothing to do with Content-Type. The website you are trying to access (https://pubmed.ncbi.nlm.nih.gov/?callback=...) simply denies Cross-Origin Read by not setting any Access-Control-Allow-Origin.

The Cross-Origin access control enforced by browsers is important mechanism against Cross-Site Request Forgery (CSRF).

You need to run your code on your own server to make the GET request on the server side instead of in the browser JS engine.