jquery 当内容类型为text/html时,如何访问第三方网站的响应?使用java脚本

1bqhqjot  于 2023-05-28  发布在  jQuery
关注(0)|答案(1)|浏览(172)

我试图得到我的得到这个网站的请求的响应https://pubmed.ncbi.nlm.nih.gov/?term=hello我得到错误:

Request failed with status code 200

警告:

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.

代码:

<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>

我可以访问,但内容被CORB阻止。本网站给出的内容类型为text/html; charset=utf-8。如何更改此代码
我试着改变内容类型没有工作,我注意到有人改变了网站内容的xml到json,并得到作为响应。但我不确定我正在使用的webiste是否有这样的json格式或jsoncallback

8ljdwjyq

8ljdwjyq1#

此错误与Content-Type无关。您尝试访问的网站(https://pubmed.ncbi.nlm.nih.gov/?callback=...)通过不设置任何Access-Control-Allow-Origin而拒绝Cross-Origin Read
浏览器实施的跨域访问控制是针对Cross-Site Request Forgery (CSRF)的重要机制。
您需要在自己的服务器上运行代码,以便在服务器端而不是在浏览器JS引擎中发出GET请求。

相关问题